elnabo / json2object

Type safe Haxe/JSON (de)serializer
MIT License
66 stars 17 forks source link

empty constructor requirement for classes #29

Closed nadako closed 6 years ago

nadako commented 6 years ago

It seems a bit unfortunate that to deserialize a class instance, a public empty constructor is required, especially considering that Haxe doesn't support (constructor) overloading.

How about using Type.createEmptyInstance instead? I believe this method was specifically added for deserializers so one can easily create an unitialized instance to fill fields later.

ibilon commented 6 years ago

Make sense. I also found this an issue, well annoying, when doing a data only class and you have to add a constructor which does nothing.

Can final fields be set by reflection? Right now the lib assume the constructor will set it.

ibilon commented 6 years ago

The library doesn't require a constructor anymore.

As a side effect code like this:

class T {
  public var v : Int = 4;
}

doesn't affect the result of the parsing. If the default value is needed in case the variable is missing from the json @:default should be used:

class T {
  @:default(4) public var v : Int;
}