elnabo / json2object

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

Using @:jcustomparse annotation causes compilation error #67

Closed jstolarek closed 4 years ago

jstolarek commented 4 years ago

Consider the following program (taken from the readme + hxjsonast qualifier on Json data type + a main function):

class Object {
  @:jcustomparse(Object.customParse)
  @:jcustomwrite(Object.customWrite)
  public var value:Date;

  public function new() {}

  public static function customWrite(v:Date):String {
    return v.getTime() + '';
    }

  public static function customParse(val:hxjsonast.Json, name:String):Date {
    return switch (val.value) {
      case JString(s):
        Date.fromString(s);
        case JNumber(s):
          Date.fromTime(Std.parseFloat(s));
          default:
            null;

            }
    }
}

class Test {
  static function main() {
    var parser = new json2object.JsonParser<Object>();
  }
}

Compiling this yields an error on the @:jcustomparse annotation:

test/Test.hx:4: characters 18-24 : Type not found : Object
~/foo/.haxelib/json2object/3,8,0/src/json2object/reader/DataBuilder.hx:930: lines 930-953 : Defined in this class

@:jcustomwrite causes no such problems though.

elnabo commented 4 years ago

This is a problem from Haxe.

In the @:jcustom* metadata, you need to specify the full path of the class you are using, like package.Object.

In your case you have put both file in Test.hx without package, thus currently the only way to access Object is to use std.Test.Object.customParse.

The problem didn't appear for @:jcustomwrite because it hasn't been use since you don't have a JsonWriter of Object

I'll update soon the readme to try and prevent such problems.

elnabo commented 4 years ago

The readme have been updated.

Note: the std. requirement if you don't have package should have been fixed with https://github.com/HaxeFoundation/haxe/issues/9367