xson 1.0.4

Xson is a pure library for resolving json, bean generating will move to xson_builder.

Usage #

Xson used like Gson.

Json member #

classmirror type
JsonElementany
JsonNullnull
JsonPrimitiveint, double, bool and String
JsonObjectMap
JsonArrayList

other #

classdescription
JsonSerializerjson serialize
JsonDeserializerjson deserialize
JsonFormatterformat json string
JsonInforesolve json structure to md5
JsonTypeenum of json type

A simple usage example:

import 'package:xson/xson.dart';

var jsonString1 = """
{"aa":100, "b":[1,2,3], "c":{"c1":[666]}}
""";

var json1 = {
  "aa": 100,
  "b": [1, 2, 3],
  "c": {
    "c1": [666]
  }
};

void main() {
  // string to json element

  // way1
  JsonElement element1 = xson.decodeToJsonElement(jsonString1);
  // way2
  JsonElement element2 = JsonElement.fromJsonString(jsonString1);
  // way3
  JsonElement element3 = JsonElement.fromJson(json1);

  // element to other type
  JsonObject object = element1.asObject; // {"aa": 100, "b": [1, 2, 3], "c": {"c1": [666]}};
  JsonArray array = object["b"].asArray; // [1,2,3]
  array[0].asInt; // 1

  // element to string
  print(element1.toString());
  print(element1.toJsonString(prettify: true, indent: 4));
  print(xson.encodeFromJsonElement(element1, prettify: true));

  // element to json dynamic
  element1.toJson();

  // traversal all nodes of element
  JsonSerializeCallback callback = (JsonElement node, dynamic parent, int depth) {
    // do something
  };
  element1.traversalChildrenNode(callback);
}

0.0.1 #

  • Initial version, created by Stagehand

0.0.6+3 #

  • change named rule of nested class name, prefix top-level class name to every nested class

  • update json serialize/deserialize interface of generated bean:

    • factory Bean.fromJson(dynamic json);

    • dynamic toJson();

dynamic must be list(json array) or map(json object), if neither is then an error will be thrown

0.0.6+4 #

  • new rule of named mode, all illegal character will be replaced with "_"

ex:

"an-apple" - "an_apple" - "anApple"

"an..apple****pie" - "an_apple_pie" - "anApplePie"

"100...test" - "$100_test" - "$100Test"

0.0.6+5 #

  • fixed bugs

0.0.7 #

  • type auto transformer of all property

  • now you can specific property type at json source just generate some tag anywhere like this:

{
    "a":100,
    "b":[
        {
            "c": 200
        }
    ]
}
//@.a=String
//@.b[].c=String

0.0.7+5 #

  • fixed bugs of nested List parsed

0.0.8 #

  • fixed transformer of List

0.0.8+1 #

  • bug fixed

0.0.8+2 #

  • bug fixed

0.0.8+3 #

  • bug fixed

1.0.0 #

  • Now xson is a pure library for resolving json, bean generating will move to xson_builder

1.0.1 #

JsonElement #

  • new getter isInt, isDouble, isBool and isString

  • new getter type, can get JsonType of JsonElement

JsonArray #

  • remove getter asNum, asString, asDouble, asInt and asBool

JsonObject #

  • new function addProperty, can add any type value

JsonPrimitive #

  • new named constructor ofInt and ofDouble

JsonInfo #

  • fixed == operator function

  • new toString(), it will return md5

1.0.2 #

  • new parent, ancestor and isAncestor in JsonElement, you can use these to traversal any node on a json tree simply.

1.0.3 #

  • new nativeType in JsonType

1.0.4 #

  • JsonInfo support JsonElement parsed

example/xson_example.dart

import 'package:xson/xson.dart';

var jsonString1 = """
{"aa":100, "b":[1,2,3], "c":{"c1":[666]}}
""";

var json1 = {
  "aa": 100,
  "b": [1, 2, 3],
  "c": {
    "c1": [666]
  }
};

void main() {
  // string to json element

  // way1
  JsonElement element1 = xson.decodeToJsonElement(jsonString1);
  // way2
  JsonElement element2 = JsonElement.fromJsonString(jsonString1);
  // way3
  JsonElement element3 = JsonElement.fromJson(json1);

  // element to other type
  JsonObject object = element1.asObject; // {"aa": 100, "b": [1, 2, 3], "c": {"c1": [666]}};
  JsonArray array = object["b"].asArray; // [1,2,3]
  array[0].asInt; // 1

  // element to string
  print(element1.toString());
  print(element1.toJsonString(prettify: true, indent: 4));
  print(xson.encodeFromJsonElement(element1, prettify: true));

  // element to json dynamic
  element1.toJson();

  // traversal all nodes of element
  JsonSerializeCallback callback = (JsonElement node, dynamic parent, int depth) {
    // do something
  };
  element1.traversalChildrenNode(callback);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  xson: ^1.0.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:xson/xson.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
64
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Feb 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Format lib/src/core/json_deserializer.dart.

Run dartfmt to format lib/src/core/json_deserializer.dart.

Format lib/src/core/json_formatter.dart.

Run dartfmt to format lib/src/core/json_formatter.dart.

Format lib/src/core/json_info.dart.

Run dartfmt to format lib/src/core/json_info.dart.

Fix additional 7 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/core/json_serializer.dart (Run dartfmt to format lib/src/core/json_serializer.dart.)
  • lib/src/core/json_type.dart (Run dartfmt to format lib/src/core/json_type.dart.)
  • lib/src/core/xson_core.dart (Run dartfmt to format lib/src/core/xson_core.dart.)
  • lib/src/element/json_array.dart (Run dartfmt to format lib/src/element/json_array.dart.)
  • lib/src/element/json_element.dart (Run dartfmt to format lib/src/element/json_element.dart.)
  • lib/src/element/json_object.dart (Run dartfmt to format lib/src/element/json_object.dart.)
  • lib/src/element/json_primitive.dart (Run dartfmt to format lib/src/element/json_primitive.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
convert ^2.1.1 2.1.1
crypto ^2.1.3 2.1.4
Transitive dependencies
charcode 1.1.3
collection 1.14.12
typed_data 1.1.6