diff --git a/README.md b/README.md index b4220a8..d5964f9 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,20 @@ White; "White" ``` +You can rename the JSON key with the @:json meta, for example: + +```haxe +enum Color { + @:json('my_fancy_rgb') Rgb(a:Int, b:Int, c:Int); + // else remain the same +} + +// will produce: + +{ "my_fancy_rgb": { "a": 0, "b": 255, "c": 128}} +``` + + This is nice in that it is a pretty readable and close to the original. However you may want to use enums to consume 3rd party data in a typed fashion. diff --git a/haxe_libraries/tink_streams.hxml b/haxe_libraries/tink_streams.hxml index 0db9043..3d33804 100644 --- a/haxe_libraries/tink_streams.hxml +++ b/haxe_libraries/tink_streams.hxml @@ -1,4 +1,6 @@ -# @install: lix --silent download "gh://github.com/haxetink/tink_streams#f4478825ef0a30df1187f02a354ec61176b47b8b" into tink_streams/0.3.3/github/f4478825ef0a30df1187f02a354ec61176b47b8b +# @install: lix --silent download "gh://github.com/haxetink/tink_streams#5066a96c4a8b483479b6a8df8893eaf8922d3bea" into tink_streams/0.4.0/github/5066a96c4a8b483479b6a8df8893eaf8922d3bea -lib tink_core --cp ${HAXE_LIBCACHE}/tink_streams/0.3.3/github/f4478825ef0a30df1187f02a354ec61176b47b8b/src --D tink_streams=0.3.3 \ No newline at end of file +-cp ${HAXE_LIBCACHE}/tink_streams/0.4.0/github/5066a96c4a8b483479b6a8df8893eaf8922d3bea/src +-D tink_streams=0.4.0 +# temp for development, delete this file when pure branch merged +-D pure \ No newline at end of file diff --git a/haxe_libraries/tink_testrunner.hxml b/haxe_libraries/tink_testrunner.hxml index d5a2d6f..d09dc08 100644 --- a/haxe_libraries/tink_testrunner.hxml +++ b/haxe_libraries/tink_testrunner.hxml @@ -1,6 +1,7 @@ -# @install: lix --silent download "gh://github.com/haxetink/tink_testrunner#866de8b991be89b969825b0c0f5565d51f96a6f7" into tink_testrunner/0.8.0/github/866de8b991be89b969825b0c0f5565d51f96a6f7 +# @install: lix --silent download "gh://github.com/haxetink/tink_testrunner#45f704215ae28c3d864755036dc2ee63f7c44e8a" into tink_testrunner/0.9.0/github/45f704215ae28c3d864755036dc2ee63f7c44e8a -lib ansi -lib tink_macro -lib tink_streams --cp ${HAXE_LIBCACHE}/tink_testrunner/0.8.0/github/866de8b991be89b969825b0c0f5565d51f96a6f7/src --D tink_testrunner=0.8.0 \ No newline at end of file +-cp ${HAXE_LIBCACHE}/tink_testrunner/0.9.0/github/45f704215ae28c3d864755036dc2ee63f7c44e8a/src +-D tink_testrunner=0.9.0 +--macro addGlobalMetadata('ANSI.Attribute', "@:native('ANSIAttribute')", false) \ No newline at end of file diff --git a/haxe_libraries/travix.hxml b/haxe_libraries/travix.hxml index b09e4f4..29c0d68 100644 --- a/haxe_libraries/travix.hxml +++ b/haxe_libraries/travix.hxml @@ -1,7 +1,7 @@ -# @install: lix --silent download "gh://github.com/back2dos/travix#354c2b2a82cc3b03e2f87cc1b6f0ddc0a6a5c133" into travix/0.15.0/github/354c2b2a82cc3b03e2f87cc1b6f0ddc0a6a5c133 -# @post-install: cd ${HAXE_LIBCACHE}/travix/0.15.0/github/354c2b2a82cc3b03e2f87cc1b6f0ddc0a6a5c133 && haxe -cp src --run travix.PostDownload -# @run: haxelib run-dir travix ${HAXE_LIBCACHE}/travix/0.15.0/github/354c2b2a82cc3b03e2f87cc1b6f0ddc0a6a5c133 +# @install: lix --silent download "gh://github.com/back2dos/travix#63b230b854c6f02cf2ac04bb758c09751f28b8e8" into travix/0.15.0/github/63b230b854c6f02cf2ac04bb758c09751f28b8e8 +# @post-install: cd ${HAXE_LIBCACHE}/travix/0.15.0/github/63b230b854c6f02cf2ac04bb758c09751f28b8e8 && haxe -cp src --run travix.PostDownload +# @run: haxelib run-dir travix ${HAXE_LIBCACHE}/travix/0.15.0/github/63b230b854c6f02cf2ac04bb758c09751f28b8e8 -lib tink_cli --cp ${HAXE_LIBCACHE}/travix/0.15.0/github/354c2b2a82cc3b03e2f87cc1b6f0ddc0a6a5c133/src +-cp ${HAXE_LIBCACHE}/travix/0.15.0/github/63b230b854c6f02cf2ac04bb758c09751f28b8e8/src -D travix=0.15.0 --macro travix.Macro.setup() \ No newline at end of file diff --git a/src/tink/json/macros/GenReader.hx b/src/tink/json/macros/GenReader.hx index 7cded0d..d8a6034 100644 --- a/src/tink/json/macros/GenReader.hx +++ b/src/tink/json/macros/GenReader.hx @@ -305,37 +305,45 @@ class GenReader extends GenBase { c = c.ctor, name = c.name, hasArgs = !c.type.reduce().match(TEnum(_,_)); + + + function mkCase(jsonKey:String) { + + add({ + name: jsonKey, + optional: true, + type: mkComplex(cfields).toType().sure(), + pos: c.pos, + access: { get: 'default', set: 'default' }, + }); + + cases.push({ + values: [macro { $jsonKey : o }], + guard: if (nullable) macro true else macro o != null, + expr: { + var args = + if (inlined) [macro o]; + else [for (f in cfields) { + var name = f.name; + macro o.$name; + }]; + + switch args { + case []: macro ($i{name} : $ct); + case _: macro ($i{name}($a{args}) : $ct); + } + } + }); + } switch c.meta.extract(':json') { case [] if(!hasArgs): argLess.push(new Named(name, name)); case []: - - add({ - name: name, - optional: true, - type: mkComplex(cfields).toType().sure(), - pos: c.pos, - access: { get: 'default', set: 'default' }, - }); - - cases.push({ - values: [macro { $name : o }], - guard: if (nullable) macro true else macro o != null, - expr: { - var args = - if (inlined) [macro o]; - else [for (f in cfields) { - var name = f.name; - macro o.$name; - }]; - - switch args { - case []: macro ($i{name} : $ct); - case _: macro ($i{name}($a{args}) : $ct); - } - } - }); + mkCase(name); + + case [{ params:[{ expr: EConst(CString(jsonKey)) }]}] if(hasArgs): + mkCase(jsonKey); case [{ params:[{ expr: EConst(CString(v)) }]}] if(!hasArgs): argLess.push(new Named(name, v)); diff --git a/src/tink/json/macros/GenWriter.hx b/src/tink/json/macros/GenWriter.hx index c675033..7fe121a 100644 --- a/src/tink/json/macros/GenWriter.hx +++ b/src/tink/json/macros/GenWriter.hx @@ -230,6 +230,11 @@ class GenWriter extends GenBase { postfix = '}}'; '{"$name":' + if (nullable) '' else '{'; + + case [{ params:[{ expr: EConst(CString(name)) }] }]: + + postfix = '}}'; + '{"$name":' + if (nullable) '' else '{'; case _ if (nullable): diff --git a/tests/ParserTest.hx b/tests/ParserTest.hx index a209709..dd63322 100644 --- a/tests/ParserTest.hx +++ b/tests/ParserTest.hx @@ -259,6 +259,12 @@ class ParserTest { return asserts.done(); } + public function renameConstructor() { + asserts.assert(parse(('{"a":{"v":1}}':RenameConstructor)).match(Success(RenameConstructor.A(1)))); + asserts.assert(parse(('{"b":{"v":"b"}}':RenameConstructor)).match(Success(RenameConstructor.B({v:'b'})))); + return asserts.done(); + } + public function primitiveAbstract() { asserts.assert(parse(('1':IntAbstract)).match(Success(IntAbstract.A))); asserts.assert(parse(('{"a":1}':{a:IntAbstract})).match(Success({a:IntAbstract.A}))); @@ -271,7 +277,7 @@ class ParserTest { } #if js - public function jsBigInt() { + public function jsBigInt() { switch parse(('{"id":-1001462968246}':{id:Int})) { case Success(o): asserts.assert(o.id == -1001462968246); diff --git a/tests/Types.hx b/tests/Types.hx index d846366..9175789 100644 --- a/tests/Types.hx +++ b/tests/Types.hx @@ -205,6 +205,11 @@ enum EnumAbstractIntKey { @:json({type: Types.MyEnumAbstractInt.B}) B(v:String); } +enum RenameConstructor { + @:json('a') A(v:Int); + @:json('b') B(b:{v:String}); +} + enum abstract IntAbstract(Int) { var A = 1; } diff --git a/tests/WriterTest.hx b/tests/WriterTest.hx index b5b8103..f7df789 100644 --- a/tests/WriterTest.hx +++ b/tests/WriterTest.hx @@ -232,6 +232,12 @@ class WriterTest { asserts.assert(stringify({foo:VeryPrivate.B}) == '{"foo":1}'); return asserts.done(); } + + public function renameConstructor() { + asserts.assert(stringify(RenameConstructor.A(1)) == '{"a":{"v":1}}'); + asserts.assert(stringify(RenameConstructor.B({v:'b'})) == '{"b":{"v":"b"}}'); + return asserts.done(); + } public function testIssue67() {