json_codec library
Descriptor-driven Proto3 JSON marshaling and unmarshaling.
Converts between Dart Map<String, Object?> message representations and
Proto3 JSON format, using the same field descriptor lists as the binary
marshal.dart / unmarshal.dart codecs.
Field descriptor format (same as binary marshal/unmarshal):
{
'name' : String, // snake_case field name
'number' : int, // protobuf field number
'type' : String, // protobuf type: 'TYPE_INT32', 'TYPE_STRING', etc.
'label' : String?, // 'LABEL_REPEATED' for repeated fields
'typeName' : String?, // fully-qualified enum/message type name
'mapEntry' : bool?, // true if this is a map<K,V> field
'keyType' : String?, // map key type
'valueType': String?, // map value type
'messageDescriptor': List<Map<String, Object?>>?, // sub-message descriptor
'enumValues': Map<int, String>?, // enum ordinal -> name mapping
}
Proto3 JSON rules implemented:
- Field names: snake_case -> lowerCamelCase in output; accept both on input.
- int64/uint64: encoded as JSON strings (too large for JS
Number). - bytes: encoded as base64 strings.
- float/double: NaN -> "NaN", Infinity -> "Infinity", -Infinity -> "-Infinity".
- enum: encoded as string name (first enum value name for ordinal 0).
- bool: true/false.
- message: nested JSON object.
- repeated: JSON array.
- map: JSON object (keys always strings in JSON).
- Default values are omitted from output.
References:
Functions
-
ensureDefaults(
Map< String, Object?> message, List<Map< descriptor) → Map<String, Object?> >String, Object?> -
Returns a copy of
messagewith missing fields filled in with proto3 default values according todescriptor. -
fieldFromJson(
Object? jsonValue, String type, {Map< int, String> ? enumValues, List<Map< ? messageDescriptor, Map<String, Object?> >String, String> ? features}) → Object? - Converts a Proto3 JSON value back to its protobuf field representation.
-
fieldToJson(
Object? value, String type, {Map< int, String> ? enumValues, List<Map< ? messageDescriptor, Map<String, Object?> >String, String> ? features}) → Object? -
Converts a single protobuf field
valueto its Proto3 JSON representation. -
isDefaultValue(
Object? value, String type) → bool -
Returns
trueifvalueis the proto3 default for the given fieldtype. -
marshalJson(
Map< String, Object?> message, List<Map< descriptor) → StringString, Object?> > - Marshals a message to a Proto3 JSON string.
-
toCamelCase(
String snakeCase) → String - Converts a snake_case field name to lowerCamelCase.
-
toSnakeCase(
String camelCase) → String - Converts a lowerCamelCase name to snake_case.
-
unmarshalJson(
String jsonString, List< Map< descriptor) → Map<String, Object?> >String, Object?> - Unmarshals a Proto3 JSON string into a message map.