开发者

XML replacement

开发者 https://www.devze.com 2023-01-25 02:38 出处:网络
I have a server application that I am rewriting in C++ and this used to use XML to send data to a client/from a client.I have found it to be a real pain to implement XML, even using existing libraries

I have a server application that I am rewriting in C++ and this used to use XML to send data to a client/from a client. I have found it to be a real pain to implement XML, even using existing libraries. It seems that it is just counter intuitive at times and the C++ library I've used seems so overly complicated.

I was wondering if anyone knew any better ways to send over data from client to server and back in a simpler and more intuitive way then parsing XML. The data consists mostly of only basic types.

I was th开发者_开发百科inking maybe just use a struct with the needed data types and just send it over a raw socket.

I have wasted so much time on this, it's unreal.


As duffymo said, JSON is perfectly suited to what you're trying to do. To add to his answer, it has healthy representation in many languages (I know XML does aswell, but the point here is that the OP is finding XML to be a pain for simple data types).

You can find out more about JSON at http://www.json.org/. At the bottom of this page are links to implementations for various languages (including C++, e.g: http://sourceforge.net/projects/jsoncpp/ ).

Put simply, JSON stands for JavaScript Object Notation and is a simple way of describing objects. An address could be respresented by:

{
    "address1" : "5, The Green",
    "address2" : "Some Street",
    "town"     : "JsonVille"
}

Arrays are supported:

{
    "address1" : "5, The Green",
    "address2" : "Some Street",
    "town"     : "JsonVille"
    "occupants": ["olivia", "newton", "john"]
}

And more complex objects:

{
    "address1" : "5, The Green",
    "address2" : "Some Street",
    "town"     : "JsonVille"
    "occupants": [{"name" : "Olivia",
                   "age"  : 24
                 },{"name" : "Newton",
                    "age"  : 32
                 },{"name" : "John",
                    "age"  : 42}
                 ]
}

EDIT: syntax error :)


I'd try JSON or Google's protocol buffers to see if they work for you.


I would look at DBus as the protocol, as it is pretty standard.


There are some alternatives depending on what your need is.

Clearly, the better alternative for performance-oriented network data passing might be Google Protobuf. However it's clearly less easy to use than any other alternative I'll give you because it's clearly made to be efficient, not human readable etc.

For human-readable data, there is:

  • JSON
  • YAML (alsmot all languages have at least one parser implementation)
  • INFO format provided by boost::property_tree


If all you are doing is sending data over a network -- you don't actually need an editable text representation such as XML or JSON -- then take a look at boost::serialization.

0

精彩评论

暂无评论...
验证码 换一张
取 消