Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @:json('name') as a way to specify the json key #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions haxe_libraries/tink_streams.hxml
Original file line number Diff line number Diff line change
@@ -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
-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
7 changes: 4 additions & 3 deletions haxe_libraries/tink_testrunner.hxml
Original file line number Diff line number Diff line change
@@ -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
-cp ${HAXE_LIBCACHE}/tink_testrunner/0.9.0/github/45f704215ae28c3d864755036dc2ee63f7c44e8a/src
-D tink_testrunner=0.9.0
--macro addGlobalMetadata('ANSI.Attribute', "@:native('ANSIAttribute')", false)
8 changes: 4 additions & 4 deletions haxe_libraries/travix.hxml
Original file line number Diff line number Diff line change
@@ -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()
60 changes: 34 additions & 26 deletions src/tink/json/macros/GenReader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions src/tink/json/macros/GenWriter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
8 changes: 7 additions & 1 deletion tests/ParserTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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})));
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tests/Types.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/WriterTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down