decoder topic

Decode a JSON String to a JsonObject. The decoder will create all actual dart types, including nested lists and/or objects. Any meaningless whitespace/formatting will be removed before parsing. Every JSON node is wrapped in a JsonElement, containing the key and data. See AST for more information about how types are mapped from JSON to dart.

One of the main features of Squint is it's decoding ability. Using dart:convert library, the following will result in an exception (type 'List<dynamic>' is not a subtype of type 'List<List<String>>' in type cast):

final json = '''
  {
      "multiples" : [
          [
              "hooray!"
          ]
      ]
  }
''';

List<List<String>> multiples = 
    jsonDecode(json)['multiples'] as List<List<String>>;

Every sublist requires casting to return the proper Dart type:

    List<List<String>> multiples = (jsonDecode(json)['multiples'] as List<dynamic>)
      .map((dynamic e) => (e as List<dynamic>)
        .map((dynamic e) => e as String)
        .toList()
    ).toList();

Squint automatically returns the correct data type. Given this JSON containing Lists, inside Lists, inside another List:

{
    "xyz": [[["hi !", "aye" ], ["bye", "zzz"]] ]
}

Decoding it requires no extra casting:

final json = """
        {
            "xyz": [[["hi !", "aye" ], ["bye", "zzz"]] ]
        }""";

final decoded = json.jsonDecode;

final xyzArray = decoded.array<dynamic>("xyz");
expect(xyzArray.key, "xyz");

final dynamic data = xyzArray.data;
expect(data[0][0][0], "hi !");
expect(data[0][0][1], "aye");
expect(data[0][1][0], "bye");
expect(data[0][1][1], "zzz");

Bad formatting is ignored:

final json = """
        {
            "xyz": [
            [[
            "hi !", "aye" 
            
            
      ], [
      "bye"
      , "zzz"
      
      
      ]         ] 
      
      ]
        }""";

final decoded = json.jsonDecode;

final xyzArray = decoded.array<dynamic>("xyz");
expect(xyzArray.key, "xyz");

final dynamic data = xyzArray.data;
expect(data[0][0][0], "hi !");
expect(data[0][0][1], "aye");
expect(data[0][1][0], "bye");
expect(data[0][1][1], "zzz");

How to retrieve String, Boolean, Integer, Double, List and Map values:

const json = """
{
  "id": 1,
  "fake-data": true,
  "real_data": false,
  "greeting": "Welcome to Squint!",
  "instructions": [
    "Type or paste JSON here",
    "Or choose a sample above",
    "squint will generate code in your",
    "chosen language to parse the sample data"
  ],
  "numbers": [22, 4.4, -15],
  "objective": { 
    "indicator": false,
    "instructions": [false, true, true, false]
  },
  "objectList": [
    { "a" : 1 }
  ]
}""";

final decoded = json.jsonDecode;

final id = decoded.integer("id");
expect(id.key, "id");
expect(id.data, 1);

final fakeData = decoded.boolean("fake-data");
expect(fakeData.key, "fake-data");
expect(fakeData.data, true);

final realData = decoded.boolean("real_data");
expect(realData.key, "real_data");
expect(realData.data, false);

final greeting = decoded.string("greeting");
expect(greeting.key, "greeting");
expect(greeting.data, "Welcome to Squint!");

final instructions = decoded.array<String>("instructions");
expect(instructions.key, "instructions");
expect(instructions.data[0], "Type or paste JSON here");
expect(instructions.data[1], "Or choose a sample above");
expect(instructions.data[2], "squint will generate code in your");
expect(instructions.data[3], "chosen language to parse the sample data");

final numbers = decoded.array<double>("numbers");
expect(numbers.key, "numbers");
// If any number in a List is a floating point,
// then they are all stored as double values.
expect(numbers.data[0], 22.0);
expect(numbers.data[1], 4.4);
expect(numbers.data[2], -15);

final objective = decoded.object("objective");
expect(objective.key, "objective");
expect(objective.boolean("indicator").data, false);
expect(objective.array<bool>("instructions").data[1], true);

final objectList = decoded.array<dynamic>("objectList");
expect(objectList.key, "objectList");
expect(objectList.data[0]["a"], 1);

Classes

ArrayDecodingKeyGenerator decoder
Generate a key which describes the exact position of a value inside a nested List.
ArrayDecodingKeyGenerator decoder
Generate a key which describes the exact position of a value inside a nested List.
ArrayDecodingKeyGenerator decoder
Generate a key which describes the exact position of a value inside a nested List.
BracketCounter decoder
Utility to count brackets and determine which content is inside the same bracket pairs.
BracketCounter decoder
Utility to count brackets and determine which content is inside the same bracket pairs.
BracketCounter decoder
Utility to count brackets and determine which content is inside the same bracket pairs.
JsonArray<T> ast decoder encoder
A JSON element containing an Array value.
JsonArray<T> ast decoder encoder
A JSON element containing an Array value.
JsonArray<T> ast decoder encoder
A JSON element containing an Array value.
JsonArrayOrNull<T> ast decoder encoder
A JSON element containing an Array value or null.
JsonArrayOrNull<T> ast decoder encoder
A JSON element containing an Array value or null.
JsonArrayOrNull<T> ast decoder encoder
A JSON element containing an Array value or null.
JsonBoolean ast decoder encoder
A JSON element containing a bool value.
JsonBoolean ast decoder encoder
A JSON element containing a bool value.
JsonBoolean ast decoder encoder
A JSON element containing a bool value.
JsonBooleanOrNull ast decoder encoder
A JSON element containing a bool value or null.
JsonBooleanOrNull ast decoder encoder
A JSON element containing a bool value or null.
JsonBooleanOrNull ast decoder encoder
A JSON element containing a bool value or null.
JsonDecode<T, R> decoder encoder
Decode a JsonNode to a non-standard dart type.
JsonDecode<T, R> decoder encoder
Decode a JsonNode to a non-standard dart type.
JsonDecode<T, R> decoder encoder
Decode a JsonNode to a non-standard dart type.
JsonEncode<T> decoder encoder
Encode a (non-standard) dart type to a JsonNode.
JsonEncode<T> decoder encoder
Encode a (non-standard) dart type to a JsonNode.
JsonEncode<T> decoder encoder
Encode a (non-standard) dart type to a JsonNode.
JsonFloatingNumber ast decoder encoder
A JSON element containing a double value.
JsonFloatingNumber ast decoder encoder
A JSON element containing a double value.
JsonFloatingNumber ast decoder encoder
A JSON element containing a double value.
JsonFloatingNumberOrNull ast decoder encoder
A JSON element containing a double value or null.
JsonFloatingNumberOrNull ast decoder encoder
A JSON element containing a double value or null.
JsonFloatingNumberOrNull ast decoder encoder
A JSON element containing a double value or null.
JsonIntegerNumber ast decoder encoder
A JSON element containing an int value.
JsonIntegerNumber ast decoder encoder
A JSON element containing an int value.
JsonIntegerNumber ast decoder encoder
A JSON element containing an int value.
JsonIntegerNumberOrNull ast decoder encoder
A JSON element containing an int value or null.
JsonIntegerNumberOrNull ast decoder encoder
A JSON element containing an int value or null.
JsonIntegerNumberOrNull ast decoder encoder
A JSON element containing an int value or null.
JsonMissing ast decoder encoder
A JSON element containing a null value and key.
JsonMissing ast decoder encoder
A JSON element containing a null value and key.
JsonMissing ast decoder encoder
A JSON element containing a null value and key.
JsonNode<T> ast decoder encoder
A single JSON node representation consisting of a String key and T data.
JsonNode<T> ast decoder encoder
A single JSON node representation consisting of a String key and T data.
JsonNode<T> ast decoder encoder
A single JSON node representation consisting of a String key and T data.
JsonNull ast decoder encoder
A JSON element containing a null value.
JsonNull ast decoder encoder
A JSON element containing a null value.
JsonNull ast decoder encoder
A JSON element containing a null value.
JsonObject ast decoder encoder
JSON Object (Map) element.
JsonObject ast decoder encoder
JSON Object (Map) element.
JsonObject ast decoder encoder
JSON Object (Map) element.
JsonObjectOrNull ast decoder encoder
JSON Object (Map) element.
JsonObjectOrNull ast decoder encoder
JSON Object (Map) element.
JsonObjectOrNull ast decoder encoder
JSON Object (Map) element.
JsonString ast decoder encoder
A JSON element containing a String value.
JsonString ast decoder encoder
A JSON element containing a String value.
JsonString ast decoder encoder
A JSON element containing a String value.
JsonStringOrNull ast decoder encoder
A JSON element containing a String or null value.
JsonStringOrNull ast decoder encoder
A JSON element containing a String or null value.
JsonStringOrNull ast decoder encoder
A JSON element containing a String or null value.
JsonValue decoder encoder
Set value of field with value of JsonNode with name.
JsonValue decoder encoder
Set value of field with value of JsonNode with name.
JsonValue decoder encoder
Set value of field with value of JsonNode with name.

Extensions

AbstractTypeFromString on String decoder
Find matching AbstractType for String value.
AbstractTypeFromString on String decoder
Find matching AbstractType for String value.
AbstractTypeFromString on String decoder
Find matching AbstractType for String value.
JsonArrayDecoder on String decoder
Decode a JSON Array String.
JsonArrayDecoder on String decoder
Decode a JSON Array String.
JsonArrayDecoder on String decoder
Decode a JSON Array String.
JsonDecoder on String decoder
Decoded a JSON String as JsonObject.
JsonDecoder on String decoder
Decoded a JSON String as JsonObject.
JsonDecoder on String decoder
Decoded a JSON String as JsonObject.
ListDecoder on List decoder
Decode a Dart List.
ListDecoder on List decoder
Decode a Dart List.
ListDecoder on List decoder
Decode a Dart List.
RepeatedBuilder on Map<String, JsonNode> decoder
Utility to construct a nested List structure with strongly typed children.
RepeatedBuilder on Map<String, JsonNode> decoder
Utility to construct a nested List structure with strongly typed children.
RepeatedBuilder on Map<String, JsonNode> decoder
Utility to construct a nested List structure with strongly typed children.

Constants

squint → const _Squint decoder encoder
All classes annotated with @squint will be processed by the squint library.
squint → const _Squint decoder encoder
All classes annotated with @squint will be processed by the squint library.
squint → const _Squint decoder encoder
All classes annotated with @squint will be processed by the squint library.

Properties

mapRegex RegExp decoder
Regex to match a Map from literal value:
final
mapRegex RegExp decoder
Regex to match a Map from literal value:
final
mapRegex RegExp decoder
Regex to match a Map from literal value:
final

Functions

buildListStructure<T>(List<List<int>> positions, {List<T>? valueList}) List decoder
Build a nested List which has all required children to set all values.
buildListStructure<T>(List<List<int>> positions, {List<T>? valueList}) List decoder
Build a nested List which has all required children to set all values.
buildListStructure<T>(List<List<int>> positions, {List<T>? valueList}) List decoder
Build a nested List which has all required children to set all values.
toJsonObject(String json) JsonObject decoder
Decode a JSON String as JsonObject.
toJsonObject(String json) JsonObject decoder
Decode a JSON String as JsonObject.
toJsonObject(String json) JsonObject decoder
Decode a JSON String as JsonObject.