jolleekin / jsonx Goto Github PK
View Code? Open in Web Editor NEWAn extended JSON library that supports the encoding and decoding of arbitrary objects.
License: BSD 2-Clause "Simplified" License
An extended JSON library that supports the encoding and decoding of arbitrary objects.
License: BSD 2-Clause "Simplified" License
I would appreciate a way to change the indent of the encoder.
A minimal project that illustrates the issue.
Jsonx emits a {}
for all dart Set
s it encounters. See the main.dart file of the above project.
The expected behavior (in my opinion, at least) would be to serialize both Sets and Lists to json arrays, since all valid Sets can be represented in json array notation. Leave it up to the program decoding this json to convert the resultant array back into a Set.
class Examination { int id; int index; String title; List<Option> options; }
class Option { int id; int index; String sequence; String content; }
Unhandled exception: NoSuchMethodError: No constructor '_GrowableList' with matching arguments declared in class 'List'. Receiver: Type: class '_GrowableList@0150898' Tried calling: new _GrowableList() Found: new _GrowableList(int) #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:196) #1 _LocalClassMirror._invokeConstructor (dart:mirrors-patch/mirrors_impl.dart:916) #2 _LocalClassMirror.newInstance (dart:mirrors-patch/mirrors_impl.dart:842) #3 _jsonToObject (package:jsonx/jsonx.dart:321:14) #4 decode (package:jsonx/jsonx.dart:184:10) #5 ExaminationJson.getInstance (file:///Users/lightwave/DartProjects/PersonalityColorTest/lib/module/ExaminationJson.dart:26:45) <asynchronous suspension> #6 main (file:///Users/lightwave/DartProjects/PersonalityColorTest/lib/module/ExaminationJson.dart:11:62) <asynchronous suspension> #7 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:265) #8 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:151)
There's a bug with dart2js handling of constructors with optional parameters (https://code.google.com/p/dart/issues/detail?id=15942). This prevents lists from being decoded when applications are compiled to javascript. One possible workaround is to detect that the mirror is the mirror
for the core list.
final Symbol _CORE_LIST = reflectClass(List).qualifiedName;
_jsonToObject(json, mirror) {
....
var instance;
if(_CORE_LIST == mirror.qualifiedName){
instance = reflect([]);
}else {
instance = mirror.newInstance(_EMTPY_SYMBOL, []);
}
This passes the unit tests and works in javascript.
If a map has a key that is anything but a String it fails to serialize.
Add an example to the README of importing jsonx with the @MirrorsUsed
annotation for dart2js users. This library is useful without mirrors, but it can be confusing how to import it without incurring the size penalty of an unchecked dart:mirrors
import when transpiling to JS.
Is there a way to "ignore" null values when encoding?
Can't seem to find this, but it could be an option like the indent option.
//bjarne
Using ObservableList and ObservableMap properties results in the wrong type getting created and a runtime exception being thrown (Exception: type '_LinkedHashMap' is not a subtype of type 'ObservableMap).
How would one encode and decode the below?
class SkillType {
final _value;
const SkillType._internal(this._value);
toString() => 'SkillType.$_value';
static const ULTIMATE = const SkillType._internal('ULTIMATE');
static const ACTIVE = const SkillType._internal('ACTIVE');
static const PASSIVE = const SkillType._internal('PASSIVE');
}
Given this example class:
class Test() {
String name;
SkillType type;
Test(String name, SkillType type) {
this.name = name;
this.type = type;
}
}
print(encode(new Test("lol",SkillType.PASSIVE)));
My desired result is as follows:
{
"name":"lol",
"type":"PASSIVE"
}
Hi,
I like the possibility to customize decoding, by adding a ConvertFunction(input)
to jsonToObjects
. But I miss an effective way to decode an element of input, using the rules of jsonToObjects
. I'd like to use it for classes with only a parameterized constructor.
Given are the following classes:
class A {
int value;
A(this.value);
}
class B {
A a;
B(this.a);
}
I'd like to decode a json of B:
main() {
B b = decode('{ "a": { "value": 2 } }', type: B);
print(b.a.value);
}
But this throws an exception: No constructor 'B.' declared in class 'B'.
To solve this problem, I add some ConvertFunction
s:
jsonToObjects[A] = (input) {
return new A(input['value']);
};
jsonToObjects[B] = (input) {
var a = decode(JSON.encode(input['a']), type: A);
return new B(a);
};
I need the property a
to create a new instance of B
. If I'd like to use the rules of jsonToObjects
, I first have to encode and then decode the element 'a'
of input. This seems to be unnecessary overhead to me, because the json object already exists.
You could add a simple function to your library:
jsonToObject(json, type) => _jsonToObject(json, reflectType(type));
Maybe there is a better solution for classes with only a parameterized constructor, but this would be useful anyhow.
Greetings,
Michael
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.