Use JSON Into An Object Model or Plain JSON - javascript

I'm doing a research about the subject of JSON Deserialization Into An Object Model, what do you think about it?
Would you prefer to use JSON from the server as is or converting it to Object Model (concrete JS objects)
What are the benefits of mapping it to Objects and not use the raw JSON, what are the negative aspects ?
What are the performance implications?
Our legacy developer wrote an SDK / DAL with a mapper function that traversing the JSON and make concrete objects,
you can see the implementation here : https://gist.github.com/send2moran/211a2eb19c4a7bf494e8
Would you work with the parsed JSON or prefer to use a mapping function?

Related

Is it safe to convert JSON into string for redis key-value storage?

So I read somewhere that storing object into Redis using pickle (Python) could result in malicious code running (It being binary data)
In context to this, I want to store an array of object created by end-user as cache in Redis. The best solution that I have come up with is to stringify the object and create an array of strings.
My concern is since the object will be created by the end-user is it safe to use the aforementioned method of converting JSON to BSON or work with stringify with Redis (I am using javascript)?
Data Eg:
[{"fname": "temp.txt", "size": 50, "type":"pdf", "mt": "Timestamp"},{..},...]
PS: I will be using javascript and not python. Also, any alternate solution which is faster/safer is welcomed.

Json object (de)serialization. Typed languages with class hierarchy

I encountered a problem with JSONs in web client-server application.
Context: scala (could be any typed language with inheritance), typescript+angularjs, json representation in NoSql postgresql, same json sent to web client.
How can I add type to json representation to get features such as:
describes generics like List
enables easy usage of inheritance in javascript/typescript world
can be deserialized line by line (like SAX) by json deserializer to get fast transformation with minimum memory used
Adding attribute to object like {myList: (...) , (...), type: ?? } interferes with point 3 due to no guarantee of attributes order.
Adding type as attribute name List#Integer#: {myList: (...) , (...)} makes the code ugly on client side due to additional wrapper/prefix everywhere.
How to solve this problem? Maybe somebody knows of Scala json library that already supports types?
Many libraries just assume that you know what type you are loading...
Workaround: Optimised deserializing is needed only on server side. For web client I transform JSON from List#Integer#: {myList: (...) , (...)} to {myList: (...) , (...), type: "List#Integer#" }
Adding type node to every object inferfers with frameworks, components i.e.: angular tree accepts only one children container at each node. Drag&drop doesn't work then. Working with flat type attribute is more predictable.

Are javascript objects and json data equivalent?

I'm learning javascript and just noticed that the syntax we use to define objects is the same as the json format. So I'm wondering if they are simply equivalent. Being more precise, does it mean that any javascript object (including its variables and functions) can be translated into json format and the same way around?
JSON is (like it's name "JavaScript Object Notation" indicates) a subset* of the JavaScript syntax, i.e. (nearly) every* JSON is valid JavaScript but not the other way round.
Functions, for example, have no equivalent representation in JSON and therefore, cannot be translated into JSON.
Since the main purpose for JSON is serialization, it doesn't provide representation of statements either.
* There is one exception: more or less all Unicode characters can be written literally in JSON but they must be written using escape sequences in JavaScript. See this blog post for more information. So not every valid JSON is valid JavaScript.
JSON data has only data specified in key-value pairs; where as js objects contain both data and functions enlisted in key-value pairs.
Think of JSON as a string representation of your javascript object. - String being the key word here.
Primarily used for easy transport over HTTP .It is a method created by Douglas Crockford to enable a friendly Javascript data transfer format similar to XML , RSS, et all.
JSON is generally parsed by server and client back into a Javascript object for use.

Python to JavaScript object graph (de)serialization

I'm trying to send an object graph from Python to JavaScript running in a browser, and I was wondering whether there is a pair of ready-to-use libraries for handling serialization on the Python side and deserialization on the JavaScript side. JSON does not support object references out of the box, the docs for JS-YAML say it's not production-ready in a browser environment, and I didn't find anything for XML. Any suggestions?
edit: Here's an example for what I mean by "JSON does not support object references out of the box": I have a shop database with products and orders and a many-to-many relationship between them. If I put a bunch of orders into the Python JSON serializer, the result will contain multiple serializations (copies) of each product, because the JSON serializer has no way of saying, "I've serialized this product already, so I'll just insert a reference to it". So I put the result on the wire and deserialize it on the client, and now I have multiple JavaScript objects representing the same product, which is bad.
how about jsonpickle? JS is awesome with json out of box, adding json pickling in python is the missing link:
jsonpickle

How do I prevent an ObjectID from becoming primitive?

How do I prevent an ObjectID form becoming primitive when passing in and out of Redis?
Parsing to JSON string? Thanks!
You can either store your object as JSON string representation in simple string or use hash data structure. Node.js node_redis module offers friendlier hash commands which may be useful. There is also a library for storing objects in redis called nohm which is based on object-hash mapping library ohm.

Categories