Are there any alternatives to JSON.decode (or any options to it I'm not aware of) that will parse the JSON with the date/time strings converted to DateTime objects?
ActiveSupport::JSON.decode({date_time: DateTime.now()}.to_json)
Having to
h["dt1"] = DateTime.strptime(h["dt1"])
h["dt2"] = DateTime.strptime(h["dt2"])
For each of the DateTime fields is quite annoying.
Or are there any other ways to serialize ruby objects as a string to store in redis and then get back the original object and their members as original objects?
Would Masrhal.dump and Marshal.load be preferred? Not sure why the redis-rb docs suggest JSON.
Unfortunately the JSON specification does not include a Date/Time data type.
You could use Marshal but that would limit you to only use ruby to write/read from redis. Perhaps a better option would be YAML, which does have a Date/Time type.
Related
Is there a JSON Deserialization equivalent of Java's Google JSON in Ruby. With-out the necessity of defining any custom serializer or deserializer for each class, one can write a one-line code to convert JSON string into custom Java class as shown here under.
Address address=gson.fromJson(addressJsonStringForm, Address.class);
To accomplish this , one need-not put any annotations/interfaces in Address class nor write separate Deserializer utility for every class that we need to deserialize. This makes it very easy to deserialize/serialize classes from third party libraries. There are quite a lot of options on whether to serialize nulls / include /exclude certain attributes etc. I 'm looking for such a versatile JSON from and to custom object serialization/deserialization utility in Ruby. I 'm new to Ruby.
Reference:
https://dzone.com/articles/deserializing-json-java-object
You can convert it into a Hash using the JSON module:
require 'json'
hash = JSON.parse('{"age":18, "name":"Vinicius"}')
hash["age"]
=> 18
If you want to convert it to a "structured" object, you can use OpenStruct:
require 'json'
require 'ostruct'
person = JSON.parse('{"age":18, "name":"Vinicius"}', object_class: OpenStruct)
person.name
=> "Vinicius"
An OpenStruct is a data structure, similar to a Hash, that allows the definition of arbitrary attributes with their accompanying values. This is accomplished by using Ruby's metaprogramming to define methods on the class itself. (docs)
OpenStruct may help you if you don't always know the JSON keys, as it dynamically creates an object.
jsonapi-rb
DeserializablePost.call(json_hash)
roar
song = Song.new(title: "Medicine Balls")
SongRepresenter.new(song).from_json('{"title":"Linoleum"}')
song.title #=> Linoleum
Netflix - fast_jsonapi
json_string = MovieSerializer.new(movie).serialized_json
I have a JSON with more or less 75 keys.
I need to receive this JSON and store offline it using Realm.
I do not want to iterate through the keys, since I've heard that there are ways to save a large JSON using a few lines. How can I do this?
EDIT:
My JSON (
I saved on a server away because it's too big)
http://myjson.com/i7e6l
There is no easy, one liner to parse the JSON and store it in Realm, since each JSON response is unique and no framework can have explicit knowledge about the structure of your JSON response without you giving some information to this framework about your JSON.
You will need to write some code either to parse the response or to make a mapping between your JSON response's fields and the properties of your Realm object. If you choose the latter solution, you can use Alamofire Object Mapper to do the JSON parsing automatically, but even then you have to write code for the mapping.
I can save a string to local browser storage with
Browser.localStorage.setItem(key, str)
but when I try to stringify a discriminated union (e.g. by calling string on it), it comes out as [Object object].
Since the FSharp.Reflection and enum-manipulation functions are not supported in Fable, how can I save and load a DU or enum value (without doing a bunch of extra work for every case)?
Per the Fable docs, you can use the function Fable.Import.JS.JSON.stringify to serialize the DU, and Fable.Import.JS.JSON.parse to deserialize it. This allows it to be saved and loaded from browser localStorage.
There is also a [<StringEnum>] attribute, which I assume makes enums be treated as their string representation, but I did not test it for this use case.
My project handles data that is stored in a key value based NoSQL database.The value part is stored as byte stream.I want a type provider to read my data according to the schema of the byte stream.The schema of the data is represented as json schema. Can I use Json type provider to read this data? If no then what can be the solution to my problem?
If your DB stores the JSON as a bytestream, simply decode it through System.Text.Encoding.UTF8.GetString (replace UTF8 with the appropriate encoding if necessary) in order to get the JSON as a regular string.
Then, you can use the JSON type provider on that stream like on any other stream, as long as you provide a compile-time sample for the type provider to use. A schema doesn't work.
In other words, you need to extract a fully representative sample of your database's JSON contents, then declare the provided types using that sample, either as a string directly embedded in the code, or as a file URI that your development machine can access.
As long as the sample matches the actual structure of your database, it will work at run-time.
// embedded in the code
type Simple1 = JsonProvider<""" { "name":"John", "age":94 } """>
// referenced
type Simple2 = JsonProvider<#"C:\MyProjectFolder\sample.json">
How can I convert any record type to a single String and back? Perhaps load the record into a stream and read it as a String? The records I'm using won't have any special types included - they're just using simple things like String, Integer, PChar, DWORD, and Array of [String], etc. and nothing like classes or functions.
This string will further be saved into various places, such as a flat text file, database record, sent over the network, etc. The string contents may be transferred by other means between each of these, such as copying the string from a text file and saving it to a database record. The general idea is that the string will be compatible enough to save anywhere, move it around, and load it back in to its original state. I do understand I need to be able to recognize what type of record it is and assign it accordingly, and that part I don't need help with.
You can serialize your record using the RTTI, from here you can use XML, JSON or other format to persist the record data.
If you don't want write your own method to serialize the records try these alternatives.
superobject (using the TSuperRttiContext class you can serialize a record to JSON)
TKBDynamic
SynCommons unit from Synopse.
XmlSerial unit (Object and Record Serialization and De-serialization to XML) from Robert Love