Coder Social home page Coder Social logo

jsonx's People

Contributors

adam-singer avatar jolleekin avatar jrote1 avatar stevenroose avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jsonx's Issues

NoSuchMethodError: No constructor '_GrowableList' with matching arguments declared in class 'List'.

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)

decoding lists throws exception in dart2js

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.

Maps with int key

If a map has a key that is anything but a String it fails to serialize.

Add @MirrorsUsed example to README for jsonx clientside use

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.

Support ObservableList and ObservableMap

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).

Encoding / Decoding Enum

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"
}

jsonToObjects: decoding in ConvertFunction

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 ConvertFunctions:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.