Jump to content
David Schwartz

Serialize/Deserialize a TObjectDictionary with JSON example

Recommended Posts

Does anybody happen to have an example of how to Serialize/Deserialize a TObjectDictionary with JSON?

 

Most of the examples I've found are for several versions ago, and I've heard that the JSON classes keep improving.

 

There seems to be two or three different approaches, none of which strike me as very straightforward.

 

The class I'm using in this case contains two strings and a TObjectList of simple objects that contain two integers (similar to TPoints).

Edited by David Schwartz

Share this post


Link to post

Hi, I guess you mean TObjectDictionary<Key,Value> from Generics.Collections...

Are you looking for a general solution or you can restrict Key or Value (or both) to some more specific type?

 

Share this post


Link to post
15 hours ago, Alexander Elagin said:

Have you tried this library: https://blog.grijjy.com/2017/01/30/efficient-and-easy-to-use-json-and-bson-library/

Just a week ago I had to implement a JSON file reader - never used JSON before - and decided to try this library. It was surprisingly easy to use.

Wow!

Very powerful.

I'll try it ASAP because I'm searching a serialize/deserialize library.

 

Thanks for sharing.

Share this post


Link to post
On 11/23/2018 at 9:02 AM, Andrea Magni said:

Hi, I guess you mean TObjectDictionary<Key,Value> from Generics.Collections...

 

yes, I've got one of these with 7500 elements in it,

 

It takes a couple of minutes to build, and once it's loaded-up, it never changes. So I want to save it to disk or a BLOB in a DB and reload it later instead of having to rebuild it each time.

 

All I need is .SaveToStream and .LoadFromStream. It doesn't need to be human readable; faster is better.

Edited by David Schwartz

Share this post


Link to post

Just following-up ... I ended up doing something fairly simple to de/serialize a few of my classes. Nothing special, and it turned out to be quite easy.

 

But I've been looking over the Grijjy code and it looks like the Griggy.ProtocolBuffers unit is perfect for what I need. I may go back and replace what I'd done.

 

I could have also used BSON because there's no need to be able to read the serialized data.

 

Thanks for the suggestions. 🙂

Share this post


Link to post

Hi.
I also ended up writing my own JSON serializer/deserializer based on delphi TJsonObject. (Using another JSON class created incompatibilities with RTL).
It works on generics (TDictionary<>, TObjectDictionary<>, Tlist<>, etc), Tdataset, TStringList, enum and nested classes. Quite fast.
I needed a special TDataset handling (speed wise) that none of the library I tried solved.
Anyway, sometimes it's worth reinventing the wheel!

Edited by Clément

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×