GithubHelp home page GithubHelp logo

Comments (11)

JohnWeisz avatar JohnWeisz commented on July 22, 2024

The overload of TypedJSON.parse without a type argument will be deprecated starting from v0.2.0. To parse JSON without types, use the JSON.parse instead.

Also, supplying an object that is not an instance of a class decorated with @JsonObject to TypedJSON.stringify is also deprecated starting from v0.2.0.

from typedjson.

lgleim avatar lgleim commented on July 22, 2024

Why do you even need to supply a type at all? Wouldn't it be much nicer to just encode it in a metadata property and to detect it automatically? This also allows to correctly parse nested Objects with their respective correct type. Are you familiar with the approach taken here? http://cloudmark.github.io/Json-Mapping/

from typedjson.

JohnWeisz avatar JohnWeisz commented on July 22, 2024

@lgleim
First, because TypedJSON aims to provide a deserialization experience through expected types, not what is present in the JSON string. Once TypedJSON is fleshed out (it's a WIP, and significant progress has been made since its last releases), this is a tool to protect against arbitrary JSON.

Second, because without the constructor of the root data type it is unknown what the type of the deserialized object should be. This is how the return type of TypedJSON.parse is determined:

interface TypedJSON {
    parse<T>(json: string, constructor: new () => T): T;
}

Leaving this out would require a type-assertion, or a generic type, but that would have to be specified at all times, which is almost the same as having to supply the constructor itself. Additionally, this ensures the root class has a parameterless constructor signature, which is needed.

The new version will provide an optionally instantiable API, through which the root data type only has to be specified once.

from typedjson.

JohnWeisz avatar JohnWeisz commented on July 22, 2024

May I ask what possible use-case would require deserialization without knowing the root type?

from typedjson.

lgleim avatar lgleim commented on July 22, 2024

In various document-based databases the type of the document is stored as a json property and validated using json-schema (if at all). E.g. for CouchDB (and alike, such as Couchbase, BarrelDB,...) there is (by default but mostly configurable) a property type at the root of each json document stored in the DB.

from typedjson.

lgleim avatar lgleim commented on July 22, 2024

I fully understand if TypedJSON is solving a different issue. Nevertheless, how does TypedJSON handle nested classes of the kind

MapUtils.deserialize(Person, {
     "name": "Mark",
     "surname": "Galea",
     "age": 30,
     "address": [{
         "first-line": "Some where",
         "second-line": "Over Here",
         "city": "In This City"
     }]
})

which would be parsed to something like

<Person>{
  name: 'Mark',
  surname: 'Galea',
  age: 30,
  address: 
   [ <Address>{
       firstLine: 'Some where',
       secondLine: 'Over Here',
       city: 'In This City' 
     } 
   ] 
}

from typedjson.

JohnWeisz avatar JohnWeisz commented on July 22, 2024

a property type at the root of each json document sored in the DB

Although this is yet to be tested (in the new version at least, which I'm currently working on), but this should be possible by specifying Object as the root data type, since TypedJSON supports polymorphism. It uses the __type property by default to look up type-names through the specified known-types.

Edit

Not with object, but with a common base class from which all other documents extend.

from typedjson.

lgleim avatar lgleim commented on July 22, 2024

Good to hear! I will be looking forward for the release. Will nested Objects with different types also be handled with the upcoming release?

from typedjson.

JohnWeisz avatar JohnWeisz commented on July 22, 2024

@lgleim
Hold on for a sec, TypedJSON supports nested objects right now as well. The constructor you need to specify is only for the root type, from which child objects are traversed automatically. It can handle complex object trees, such as what is defined in https://github.com/JohnWeisz/TypedJSON/blob/master/src/tests/polymorphism.ts

from typedjson.

lgleim avatar lgleim commented on July 22, 2024

Thanks for pointing this out! Looking forward to the release.

from typedjson.

Neos3452 avatar Neos3452 commented on July 22, 2024

Hey, if you would be interested again, this was released some time ago now.

from typedjson.

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.