Comments (6)
Can you expand a little on the idea? Is Collapsed
always String | Int
and Expanded
always an object?
from tink_json.
Is Collapsed always String | Int and Expanded always an object?
Yeah exactly
from tink_json.
This is basically a Either
type. But we need some ways to distinguish the two types.
Say in the above example, the client
field is actually Either<String, ClientObject>
.
When the parser reads a "
it parse the field as a string and produce a Left
, otherwise try to parse an object and produce a Right
The idea can be expanded to allow custom ways to distinguish the types. For example, some rest api returns errors within the body. So the response type is Either<{error:String}, ClientObject>
. To make it more general, the @:json meta might be enhanced as follow:
enum Response {
@:json(String) ClientId(id:String); // use this entry when a plain string is encountered
@:json({error:String}) Error(error:{error:String}) // use this entry when it is an object with 'error' key as a string
ClientObj(obj:ClientObject); // otherwise try to parse as client object
}
from tink_json.
Well, Either
wouldn't actually be that hard. The generalization however is a bit tricky, in that you may have multiple branches for {
, which you may have to back out of.
As for the error, I know what you mean. I work with an APIs where you can get away with this:
enum Result<Data> {
@:json({ success: true }) Success(data:Data);
@:json({ success: false }) Failure(error:String);
}
from tink_json.
will @:json({error: null})
be equivalent to the non-existence of the error field?
from tink_json.
No, but I think that would actually be a good interpretation ;)
from tink_json.
Related Issues (20)
- Writer tracks `__first` in anonymous objects even when there are no optional fields.
- [nodejs] strange parser performance HOT 3
- from/to + representation HOT 1
- Field parsing should use some sort of state machine.
- Do something about nulls in writer. HOT 5
- Add validationless mode.
- Support child class / interface implementations
- Allow final class fields to be serialized? HOT 2
- Support guards in @:jsonParse
- haxe 4.2 nightly build bug. HOT 1
- Dynamic parser references and type parameters HOT 2
- Support GADT?
- State machine bug?
- parse() broken for Haxe 4 HOT 2
- Allow open enum abstracts
- GenReader fails when enum abstract is inited with enum abstract values
- `DynamicAccess<T>` does not seem to honor `@:jsonParse` defined on the concrete type substituted for `T`
- Allow parsing final @:structInit classes.
- Roundtrip of an enum doesn't work if one of the enum parameters is a haxe.ds.Option HOT 4
- tink_json cannot stringify cpp.CastCharStar
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tink_json.