9

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.

LU RD
  • 34,438
  • 5
  • 88
  • 296
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 1
    Probably not. :-) Now can you state the problem you're trying to solve, so we can suggest the proper way to solve it? What is the point of converting anything that isn't a string to a string only to convert it back? – Ken White Jun 08 '12 at 22:42
  • 2
    As a hack, you can write the record to a memory stream, as you suggest, and then convert the bytes of that memory stream to a string of hex values. That can be reversed. Of course, it's going to be hard for humans to decipher the string :) – 500 - Internal Server Error Jun 08 '12 at 22:44
  • @500-InternalServerError That's perfect, answer with a sample converting each way and I'll accept it :D – Jerry Dodge Jun 08 '12 at 22:46
  • @JerryDodge: You'll have to do that yourself, I don't happen to have Delphi running at the moment :) – 500 - Internal Server Error Jun 08 '12 at 22:48
  • If I can find how to convert a record to/from a `TMemoryStream` I can figure out the rest – Jerry Dodge Jun 08 '12 at 22:55
  • [StackOverflow is not a collaborative development environment](http://meta.stackexchange.com/a/133222/172661). Voting to close, because you're still trying to get someone to hit a moving target. Define the problem you're trying to solve, so someone can give you a solution to that problem. – Ken White Jun 09 '12 at 00:46
  • 2
    You would like to see http://stackoverflow.com/questions/3820996/delphi-2010-how-to-save-a-whole-record-to-a-file . There are a lot of good answers there but without any string conversion. You can probably incorporate that. – Sertac Akyuz Jun 09 '12 at 01:08
  • 4
    @Ken This is a reasonable question. Serialization is a very commonly used technique and many modern languages come with ready made frameworks for serializtion. RRUZ gives helpful links to such frameworks for Delphi. – David Heffernan Jun 09 '12 at 07:08
  • Jerry, there are many reasons to use the frameworks proposed by RRUZ. The records can be reviewed, values are connected to a keyword and flexible enough to later be expanded/shrinked. Going for a solution with a blob stream with hex values is a dead end in my experience. – LU RD Jun 09 '12 at 07:30
  • @500 That's pretty lame really. Binary memcpy has all sorts of problems. Only works with value types. Fails if fields are re-ordered. Fails if fields are removed or added. And then string of hex values?! Base-64 would seem more effective. Jerry, use a real serialization framework and then JSON to make it readable. My personal favourite YAML doesn't have good Delphi libs available. You need to link to libyaml yourself which is tricky. – David Heffernan Jun 09 '12 at 09:02
  • @David, sorry. In it's current form (and review the many changes it's made to get here), IMO it's not. It's gone from `any record`to `some records` to `records only containing certain data types (like string and PChar) (which of course are poor candidates for serialization. It's a reasonable question when Jerry writes it so it stops morphing from one thing to another, and he actually asks a question. When SO questions gain a medal for "Most Skeet=shoot like moving target", I'll nominate this one for it. Serialization would be a good question, but not if it's this vague and keeps changing. – Ken White Jun 09 '12 at 09:04
  • 2
    @Ken I didn't see earlier versions, so I can't comment on those. It doesn't seem so bad to me now, but there you are. Much better than recent "Learn to code with Jerry" efforts so I'd like to be encouraging! Anyway, a point of detail. Why is string a bad type to serialize? It's totally routine to do so. Also array of string. Also dictionaries. – David Heffernan Jun 09 '12 at 09:13
  • @David, as you yourself said, you didn't see the earlier versions (or the comments Jerry deleted that I was referring to above). He did indeed start off with `any recrd`, and then specifically mentioned `PChar` as one of the types he was interested in serializing. The question has improved somewhat, but don't criticize what was said about prior incarnations. The question also mentioned nothing about serialization. The original question simply said "how do I convert `any record` to string and back?" I asked for clarification of the problem being solved, and **eventually** serialization emerged. – Ken White Jun 09 '12 at 14:12

1 Answers1

8

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
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Not every type that can be contained in a record is available through RTTI, including the new enhanced RTTI. So this won't convert *any* record to a string. (Not downvoting, just commenting on the vagueness of the question that makes it difficult to answer.) How do you convert a record containing overloaded operators or methods to a string, for instance? What about records that contain other records? – Ken White Jun 08 '12 at 23:29
  • 2
    @KenWhite I used the RTTI to serialize records without problems, but Indeed, can be exist some complex records difficult to serialize, Anyway I don't know what you mean with `..How do you convert a record containing overloaded operators or methods` because when you serialize a record or object only you need persist the fields and/or properties. – RRUZ Jun 09 '12 at 00:07
  • @RRUZ: Rodrigo, my point was that the question is too vague. Does `convert a record to a string` mean to store and reload again later locally, or does it mean to transfer it to another system? Does it mean only the data (fields and properties) or the entire record structure? I didn't downvote your answer because it's probably right, but the question itself needs to be edited to make it clear what the intent (problem to be solved) is, so that a proper answer to the question can be provided. – Ken White Jun 09 '12 at 00:15
  • 1
    @RRUZ: My point was just made by Jerry's comment (to your question, right above my last): Now the requirements have changed from `any record` to `just things like String, Integer, PChar, array of something, etc.`. (And of course a `PChar` can't be serialized, because a `PChar` is a memory address.) – Ken White Jun 09 '12 at 00:21
  • 2
    @Ken PChar is a bit of a nonsense. Surely nobody routinely puts those in records. Use string instead. But all the other types are of course well supported by serialization frameworks. That's routine bread and butter work for such frameworks. – David Heffernan Jun 09 '12 at 09:03