jolleekin / jsonx

An extended JSON library that supports the encoding and decoding of arbitrary objects.
BSD 2-Clause "Simplified" License
11 stars 6 forks source link

Changed from providing a Type for every decode call to use a generic cla... #1

Closed delaneyj closed 10 years ago

delaneyj commented 10 years ago

So I saw in your documentation that you included Type type in the decode become you couldn't use a generic. I wrapped the encode/decode in a generic class and makes it so you only have to setup per class you decode once. Also I didn't include in this pull request but in this version the encode could be static.

Please consider this as a possibly cleaner syntax for using your very helpful package.

jolleekin commented 10 years ago

Hi Delaney,

If you think

var user = userJsonx.decode(text);

is cleaner than

var user = decode(text, type: User);

then, I can add the following class to the library

class Jsonx<T> {
  String encode(T object) => encode(object);
  T decode(String text) => decode(text, type: T);
}
delaneyj commented 10 years ago

On a single use case the syntax isn't very different its when you start decoding a lot is where it makes more sense. Your solution is probably more idiomatic than what I did.

Was hoping (since I haven't used dart reflection yet) that being a class instance you'd be able to cache off the call to reflectType(T). Even though I haven't benchmarked yet I figured generating a TypeMirror could be an expensive call and caching as in instance in the class for further use would make a lot of sense for most cases where you would be decoding many times to the same type.

jolleekin commented 10 years ago

I'm not sure if it's a good idea to expose both the top level methods and that new class Jsonx. But I'm sure there are people using the library, so removing the top level methods is not the case. If you don't have any other comment, I think I'll go ahead to add that class and TypeMirror caching.

delaneyj commented 10 years ago

Not sure I quite understand what you are saying. Are you saying you'd remove the top level methods and expose as part of the class? Or that you'll just add a class to do the caching of TypeMirrors?

jolleekin commented 10 years ago

I mean (1) adding the class to wrap the current top level methods and (2) adding TypeMirror caching in the top level decode().

delaneyj commented 10 years ago

Both those sound great to me. Great work on the library.

jolleekin commented 10 years ago

New version has been uploaded. I actually implemented the Codec API. Please See README.md.

delaneyj commented 10 years ago

Great job!

jolleekin commented 10 years ago

How are you using the library with DateTime?

By default, JSON doesn't support DateTime (or enums). So, I'm adding the capability of allowing users to provide custom encoders and decoders. For example, users can provide custom DateTime encoder/decoder to use ISO-8601 format or ASP.NET ["/Date(1234567890)/"] format.

customToEncodables[DateTime] = (input) => input.toString();
customToObjects[DateTime] = DateTime.parse;

Sounds good?