Convert any record to a string and back? - delphi

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

Related

Storing an data item's file extension in Core Data: plain string or other flavor of data Type?

I'm working on the data model for an app I'm building that will use CoreData. As part of that process, the app includes a function that will iterate through a series of MP3 files to populate the database.
The data model at the moment is:
var metadataArray = [(fileName: String, title: String, artist: String,
duration: String, fileExtension: String)]()
Note that in order for the app to later access those files, I'm storing the file name and the file extension. Since the path to the app's Documents folder isn't known at build time, my thought was to include code that programmatically locates the Documents folder. Then, when it's time to access a file, use the stub path to the Documents folder and tack on the filename String and fileExtension String using .appendPathComponent() to generate a URL from the three components.
My question feels kind of esoteric, but it seems like the right time to ask it:
Q: Swift provides a method to assemble URLs from strings, but in the data model declaration, is there any special data Type that's more appropriate for storing file names and file extensions than just plain Strings?
I'm guessing the answer is no -- that Strings are fine, given all the methods Swift provides to assemble full URLs from Strings. But since Swift allows you to declare a full URL as a data type, I thought it was worth asking if there's some sort of a data type more suitable for components of a URL.
Thanks in advance!

Fable/F# - How do I save enums or discriminated unions to the browser?

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.

F# type provider

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">

MVC EF String Length / Data Type for Signature Image

I'm using a javascript plugin called jSignature to give my MVC4 application delivery signature capture functionality. jSignature outputs the signature info in a data:image/png;base64,iVBORw0KG... string format that looks to be around 32,000 characters long. I created a property in my model called DeliverySignature as a string and am able to save and retrieve the signature data but when I pull it back in from the database it's only about 5,000 characters long. What data type do I need to be using in my model's definition and in the action method (it's being passed in to a controller to save to the db) so that it preservers the complete string length? Thank you.
I would store it in varchar(max) column.
You can keep model property type of string as strings do not have a limited length.

How to get back DateTime from JSON.decode

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.

Resources