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.
Related
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.
I'm new to programming, and I have been programming a small project with vanilla javascript, but I was using a lot of document.getElementById() tags, and I stored all of these in a javascript object, on a seperate file, but I was wondering If I could Just store that object on a SQL file, to make my project more organized.
I'm not sure if that's possible, I know that SQL stores data, so would I be able to store my JS object on a sql file, and import that object into my seperate Javascript files?
I'm trying to make sure if I can do what I want to do before I decide to start learning sql, but If it does do what I need, I was going to start incorporating it for organization, so I can learn it as I create projects.
You can use the JSON.stringify function to convert your javascript objects into strings. However, it is important to note that the only items within the javascript object that are converted into strings are: objects, arrays, strings, numbers, and values that are: null, true, or false. If you have references to functions or classes that have been instantiated, then these will be lost. You can convert the string back into a javascript object using JSON.parse.
One thing to consider before you do this is whether or not you need to perform database queries on the data that you are storing within the javascript object. If you need to search on the javascript object's data, then you should store the information directly within tables in the database. If you don't need to search on it, then converting the data to a string and saving it should be fine to do. Since it sounds as though you are using the data for your own purposes, doing this should be fine since extracting all of the data from the database shouldn't be an intensive task. Also, you can write your own scripts to parse the data.
Definitely, you can store as a JSON Blob
https://learn.microsoft.com/en-us/sql/relational-databases/json/store-json-documents-in-sql-tables?view=sql-server-ver15
I am currently working on a kotlin multi project solution.
I have one project defining some data classes and defining an api to access a mongodb. The objectId is created automatically. This project is using morphia:1.3.2.
Entries are stored using this function:
fun store(myClass: MyClass) = db.save(myClass).let { myClass.id?.toHexString() ?: "0" }
Now I'm using this project in a spring-boot kotlin project.
I created a small web page with some filters. These filters should be applied on my query. So far so good, everything is working.
The results of my query are returned via my Rest-controller without any conversions. In my web page I want to print the ObjectId foreach result.
But the ObjectId is not a String as it used to be, it is an object.
id:
counter:15304909
date:"2018-08-27T23:45:35.000+0000"
machineIdentifier:123456
processIdentifier:1234
time:1535413535000
timeSecond:1535413535
timestamp:1535413535
Is it possible to force morphia to return the objectId in the String representation? Or is there a on Option to activate the correct mapping? Or do I have to touch each result one by one and convert the object id to the hexadecimal string representation? I hope that there is a better, and quicker solution then this.
I am also not able to remap the object to a valid id, due to an java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 exception. The request looks like this:
myClass?id={"timestamp":1535413631,"machineIdentifier":123456,"processIdentifier":1234,"counter":16576969,"time":1535413631000,"date":"2018-08-27T23:47:11.000+0000","timeSecond":1535413631}
I'm a little bit out of ideas, how to fix this issue.
Depending on your REST framework, you would need to provided a serializer for writing out that ObjectId as its String version, say. Most such frameworks make that transparent once it's configured so you need only worry about returning your objects out of your REST service and the framework will serialize properly.
I, personally, wouldn't muck about by trying to change how it's serialized in the database. ObjectId is a pretty good _id type and I wouldn't change it.
I am having trouble finding information online as how to use Mongo ObjectID instances on the front-end.
I can't answer these questions:
(1) is it safe to serialize/deserialize ObjectID objects to/from JSON?
(2) How can I require the ObjectID module with AMD/RequireJS on the front-end?
(3) Is it better to just use strings on the front-end and just convert strings to ObjectIDs on the backend?
So yes I am having trouble working with and manipulating ObjectID objects on the frontend because I don't have the ObjectID module on the frontend, or at least this is a perceived problem. I haven't seen any examples of how to do this nor have I seen much talk of it online at all. Perhaps I am not approaching the problem correctly.
No. Your JSON parser will likely fail, as JSON only stores certain datatypes, and ObjectID isn't one of them...
Although, note that if you're stringifying your data it is possible that your MongoDB driver would in fact back this by returning a string from the ObjectID... Here is an example in NodeJS:
var ObjectID = require("mongodb").ObjectID,
myObject = {test:ObjectID("55153a8014829a865bbf700d")};
console.log(JSON.stringify(myObject));
// {"test":"55153a8014829a865bbf700d"}
No. I'm not sure there are any modules out there yet that give the ability to use ObjectID's in browser JS. Although perhaps you can port this NodeJS to browser JS compatibility?
Yes. For now, I would say yes. You can just use the string on the front-end; although, like I said ealier, if you can port the ObjectID peice to be browser compliant (which shouldn't be too hard), I don't think there would be any issues there.
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?