Comments (11)
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.
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.
@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.
May I ask what possible use-case would require deserialization without knowing the root type?
from typedjson.
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.
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.
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.
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.
@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.
Thanks for pointing this out! Looking forward to the release.
from typedjson.
Hey, if you would be interested again, this was released some time ago now.
from typedjson.
Related Issues (20)
- [Discussion] `parse`, `toPlainJson`, `toPlainArray`, `toPlainMap`, `toPlainSet` should not return `undefined` HOT 1
- Using @json[Array,Set,Map]Member with a nullable type does not work
- Parsing array/set/map of primitive values HOT 2
- Array of Enum values HOT 4
- Unable to propagate current deserialization options in custom deserializer HOT 2
- Unable to parse complex nested type HOT 3
- URGENT ! TypeThunk broke everything for Angular Production Builds HOT 14
- Support for "pure object references"
- Indent with string | number | null
- Allow lazily define type in annotations / ES modules HOT 4
- Allow @jsonObject itself to specify custom serializer/deserializer functions.
- Lazy type definition syntax for `@jsonMemberArray`doesn't resolve for circular dependencies HOT 1
- Allow ignoring fields in a @jsonObject
- Polymorphic / generic types?
- Deserialize / convert missing / undefined / null fields?
- Not getting errors when field present in class is not present on JSON when parsing HOT 1
- I don't know how to use it in cocos creator 3.5+ HOT 5
- JS: [ERROR]: ReferenceError: Cannot access 'Pos' before initialization HOT 6
- Upgrade decorator syntax to Typescript 5.0 HOT 1
- Serializing odd-length ArrayBuffer fails
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 typedjson.