Tag Archives: json
Nullable fields in json generated from protobuf file
Native support With grpc v2 for go, protofile’s optional option is supported, so you don’t need to import proto extensions anymore and use things like google.protobuf.StringValue. my.proto
1 2 3 |
message FeeView { optional string name = 1 [json_name = "name"]; } |
go mapper
1 2 3 4 5 6 7 8 |
var name *string if fee.Name != nil { name = &fee.Name } fee := &desc.FeeView{ Name: name, } |
Legacy way with extensions Null for nil value my.proto
1 2 3 4 5 |
import "google/protobuf/wrappers.proto"; message FeeView { google.protobuf.StringValue name = 1 [json_name = "name"]; } |
go mapper
1 2 3 4 5 6 7 8 9 10 11 12 |
import "github.com/gogo/protobuf/types" var name *types.StringValue if fee.Name != nil { name = &types.StringValue{ Value: *fee.Name, } } fee := &desc.FeeView{ Name: name, } |
Which will lead to this json response
1 |
{"translation_key":"a fee","name":null,"value":5,"style":"body","text_color":"grey","tag":null} |
Omit a field …
GRPC fallback to Rest API with custom field names
When you generate JSON for Rest API from proto-file, protoc-gen-gofast generates field names for JSON in lowerCamelCase format while most of the Rest APIs use snake_case for that. And if you want to replace some legacy API with your new implementation without breaking backward compatibility, you need to fix it. There could be different ways …
PostgreSQL: using indices on json fields
It’s obviously a really bad idea. But if you really need it, that’s what you can do.
JSON formatter
http://jsonformatter.curiousconcept.com/
Библиотека для удобочитаемого представления json
Удобно показывает json-объекты. Чем сложнее json, тем удобнее смотреть. http://marianoguerra.github.io/json.human.js/
Фишки JSON.stringify()
В общем тут говорится о том, что этот метод может принимать дополнительные параметры, которые могут фильтровать и форматировать вывод. http://freshbrewedcode.com/jimcowart/2013/01/29/what-you-might-not-know-about-json-stringify/
Отдача json из Django view
1 2 3 4 5 6 |
from django.http import HttpResponse import simplejson as json ... def my_view( request ): jsonDict = { "status": "success", "message": "everything's fine" } return HttpResponse( json.dumps( jsonDict ), mimetype="application/json" ) |
Подробнее о simplejson — в документации