highsource / jsonix

Powerful XML<->JSON JavaScript mapping library.
BSD 2-Clause "Simplified" License
361 stars 78 forks source link

Have an option to marshal boolean to numeric representation #218

Open hmaracic opened 5 years ago

hmaracic commented 5 years ago

The unmarshaller already understands "0" as false and "1" as true, which is correct per XML specification.

Could it be possible (if it doesn't exist already) to add some configuration option to output "0" and "1" when marshalling JSON, instead of "false" and "true"?

highsource commented 5 years ago

You can implement and register a custom booleanish type for this. Let me know if you would like to give it a try, I'll try to provide pointers.

hmaracic commented 5 years ago

That would be great, thanks!

highsource commented 5 years ago

Ok. Start by writing your own boolean type.

See:

https://github.com/highsource/jsonix/blob/master/scripts/src/main/javascript/org/hisrc/jsonix/Jsonix/Schema/XSD/Boolean.js

You can subclass Jsonix.Schema.XSD.Boolean or write one from the scratch, that's fairly simple.

highsource commented 5 years ago

The you can use your type in your mappings. An example is here:

https://github.com/highsource/jsonix/issues/157

highsource commented 5 years ago

I even think you can override the default Boolean type like this.