unmarshalJson function

Map<String, Object?> unmarshalJson(
  1. String jsonString,
  2. List<Map<String, Object?>> descriptor
)

Unmarshals a Proto3 JSON string into a message map.

Accepts both lowerCamelCase and snake_case field names in the input JSON. Fields are mapped back to their snake_case names in the output map.

Returns a Map<String, Object?> with field values decoded from JSON.

Implementation

Map<String, Object?> unmarshalJson(
  String jsonString,
  List<Map<String, Object?>> descriptor,
) {
  final jsonMap = jsonDecode(jsonString);
  if (jsonMap is! Map) {
    throw FormatException(
      'Expected a JSON object at top level, got ${jsonMap.runtimeType}',
    );
  }
  final stringMap = <String, Object?>{};
  for (final entry in jsonMap.entries) {
    stringMap[entry.key.toString()] = entry.value;
  }
  return _unmarshalFromMap(stringMap, descriptor);
}