Code Monkey home page Code Monkey logo

Comments (6)

back2dos avatar back2dos commented on August 30, 2024

Can you expand a little on the idea? Is Collapsed always String | Int and Expanded always an object?

from tink_json.

kevinresol avatar kevinresol commented on August 30, 2024

Is Collapsed always String | Int and Expanded always an object?

Yeah exactly

from tink_json.

kevinresol avatar kevinresol commented on August 30, 2024

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.

back2dos avatar back2dos commented on August 30, 2024

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.

kevinresol avatar kevinresol commented on August 30, 2024

will @:json({error: null}) be equivalent to the non-existence of the error field?

from tink_json.

back2dos avatar back2dos commented on August 30, 2024

No, but I think that would actually be a good interpretation ;)

from tink_json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.