Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Advice on writing a JSON serializer/deserializer in C

json data serialization macros generic object

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 DarkLordCthulhu

DarkLordCthulhu

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 422 posts

Posted 24 September 2015 - 03:18 PM

I'm trying to write a library to serialize and deserialize objects stored in a JSON file.  I could do it the tedious way and have each name and value pair be represented by two strings, with markers to indicate which one is the name and which one is the value, and also what data type it is, but that solution doesn't seem very elegant, so I decided to do it the hard way, by actually mapping the JSON objects to C structs, with appropriate C types determined by the JSON type.  This presents an immediate problem, which is that I don't know what fields the struct will have, since a JSON object can be anything.  So I will have to find a way to define a generic struct based on what is read from the file.  Of course, structs can't be declared within functions, so I would need to use a macro for this, and it would need to be called outside of any function.  The code I've written so far looks like this:

 

#define readobject( fp )\
struct generate_name( fp ){\
read_fields( fp )\
};
 
#define read_fields( fp )\
// Read name
// Read value
// If comma not encountered, define end_reached
#ifndef end_reached\
read_fields( fp )\
#endif\

 

Looking at the code, the problem is almost immediately obvious.  On the one hand, I have to be outside of any function in order to declare a struct, but on the other hand, I have to be inside of a function to call the I/O functions necessary for reading a file.

 

This problem is utterly beyond me.  Any advice on how to work around it?


Programming is a journey, not a destination.

#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 25 September 2015 - 06:30 AM

why reinvent the wheel?  

 

http://ccodearchive...._tokenizer.html


or

 

http://ccodearchive.net/info/json.html


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#3 DarkLordCthulhu

DarkLordCthulhu

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 422 posts

Posted 26 September 2015 - 12:20 PM

Why reinvent the wheel?  Because I like challenges, and honestly, pretty much any programming idea any of us can come up with has been implemented before by someone at some point.  But thanks for informing me about what other people have been doing with C.

 

Anyway, I decided not to map the JSON objects directly to C structs.  Instead, I'm going to have each object represented as a linked list of structs, where each struct represents a name/value pair, with the name being a string and the value being a union type with a separate field for each of the JSON data types.


Programming is a journey, not a destination.

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 29 September 2015 - 03:01 AM

It'll have to be a union with some way of indicating what the type is (i.e. an enum). So you will end up using a struct!

struct json_object {
    const char *name;
    int value_type;
    union {
        int int_value;
        double double_value;
        const char *string_value;
        json_object *object_value;
    } value;
};

sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download