Giter Club home page Giter Club logo

Comments (2)

mcdee avatar mcdee commented on June 19, 2024 1

Yeah, understand. If the types can be arbitrarily complex then objects are going to be required, it's just a pain to map JSON to an internal struct when the value can have different types (string or object).

Although it would be possible to build a full JSON representation of a CLType that was well-defined and had a single defined structure, I very much doubt that it is worth your time to do so. I'll patch something up in my parser instead.

Thanks for the detailed response.

from casper-node.

Fraser999 avatar Fraser999 commented on June 19, 2024

That's an interesting point! I agree it's possible, and might make the ergonomics slightly better for users consuming the JSON. However, there are a couple of factors at play here.

While it's true that many of the other variants of the CLType can be represented as a single JSON string field (the simple variants), it's not true that they all can. The complex variants can be comprised of nested CLTypes (except for ByteArray which contains the length of the array rather than a nested CLType). In JSON form, these complex variants need to be represented as JSON objects rather than strings, so ByteArray isn't unique in that regard.

A more practical problem with adopting the approach you suggest is that we currently just derive the Serialize and Deserialize traits for CLType. This gives us the ability to serialize to a binary format or to JSON or TOML for free.

In its binary format, the nested length of the ByteArray needs to be included so that the bytes comprising the data can be distinguished from possible other data. Normally the length is encoded as a prefix, followed by the number of bytes specified by that prefix, followed by any other encoded data.

So, on the one hand we need the Serialize and Deserialize implementations to use the nested length in ByteArray, but on the other (for JSON encoding), we want to skip it. This is possible: we could hand-write custom implementations for these traits rather than deriving them. However, the risk vs reward doesn't seem worthwhile to me in this case, given the complexity of not deriving the traits, and considering that this is only useful for single variant of the CLType.

I'll not close this issue for now so we can discuss further, but my current inclination is to not change from the status quo.

Thanks anyway for raising the issue. It's a point I hadn't considered up until now :)

from casper-node.

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.