2

Basically, I want to have an interface for converting Objects to/from their XML or JSON String representation, something like

public interface IStringifier{

/**
   Converts the Object to it's String representation, e.g. XML or JSON
*/
   public String toString(Object o);

/**
   Converts from the String representation (e.g. XML or JSON) to an Object
*/
   public Object fromString(String s, Class<?> clazz);
}

Such an interface would be fairly simple to implement in GSON, XStream etc. but by abstracting it you are abstracted from knowing just what is going on underneath. And you are decoupled from one of the many many XML or JSON libraries, so clients are freer to pick their favorite.

Is there any "standard" Java interface for this? Something in Guava, Apache, etc?


(added) None of the answers were what I really wanted ("yes, in javax.obscure.interfaces there's what you want") but thanks for the replies. I'll accept Tom's answer as the most informative/provocative. And maybe I'll clean up the code I have and try to create a standard. :-)

user949300
  • 15,364
  • 7
  • 35
  • 66
  • 2
    Google... [first](http://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java), [second](http://stackoverflow.com/questions/5113711/convert-xml-to-json-format), [third](http://stackoverflow.com/questions/7724263/how-to-convert-xml-to-json-in-java), [fourth](http://stackoverflow.com/questions/7472282/convert-xml-to-from-json-in-java-without-extra-e-and-o-elements)... – user219882 Dec 11 '12 at 00:20
  • @Tomas looks like you misunderstand the question - OP is asking about an interface precisely so that any of those different answers can be interchanged. – djechlin Dec 11 '12 at 00:38

4 Answers4

3

JAXB (JSR-222) is the Java SE/EE standard for converting objects to/from XML. It can be used standalone and is the standard binding layer for JAX-WS (SOAP) and JAX-RS (RESTful) Web Services. Below is a link to an example of specifying an alternate provider via a jaxb.properties file.

There currently isn't a standard API for JSON binding.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • The only thing there that seems even remotely close is Marshaller and Unmarshaller. But they are literally an order of magnitude too big. To quote Eran Hammer on web stuff, [he resigned from Oauth2](http://hueniverse.com/2012/07/oauth-2-0-and-the-road-to-hell/) "they are not capable of simple". – user949300 Dec 11 '12 at 22:33
1

XML and JSON are unrelated, so this is actually two questions:

For JSON, although "unofficial", a popular library is GSON.

For XML, see Blaise's answer

Community
  • 1
  • 1
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 3
    GSON is definitely ***not*** the defacto standard for JSON processing in Java. – Perception Dec 11 '12 at 00:47
  • @Perception well it's the goto library in my book. Lots of projects use it. But OK I've downgraded the language I used to describe it. – Bohemian Dec 11 '12 at 00:55
  • kudos for the edit. GSON is indeed a very popular library, but for example, Jackson has a large installed base due to its inclusion as default JSON library in both RESTEasy and Jersey. Will be interesting to see how the libraries evolve when a standard is eventually created. – Perception Dec 11 '12 at 00:59
  • @Perception (and @Bohemian). My concern/complaint/question is that _none_ of these libraries seem to implement _any_ interface, let alone a _standard_ one. The ability for some other code, lets say a general purpose REST client, to support a general, swappable JSON or XML library is therefore limited. – user949300 Dec 11 '12 at 22:41
  • Thats pretty much sums it up, @user949300 - there is currently no Java standard for processing JSON data. This is not unusual, it took years for standard API's to come out for processing XML, and even now there are several standards in play. – Perception Dec 11 '12 at 23:27
  • @Perception Thanks for the feedback, that's what I feared. Sometimes things are slow. Most of Swing still doesn't know about Collections (only Vectors if that!). – user949300 Dec 12 '12 at 00:05
1

I think you're overthinking this. You don't actually care about turning objects into Strings, you want to be able to serialize objects to different formats without knowing what that format is. But who says that different format is a String? What happens when you want your object to be available as a protocol buffer? That's a binary format, not a character format -- so stringify() won't help there. Ultimately, it's up to you to architect your application to be as independent as possible of those details.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
  • Good point. I considered generifying the Interface to handle this case, but for simplicity kept it as a String. (Obviously, one could also generify the class of the Object) – user949300 Dec 11 '12 at 03:44
0

One popular JSON-to-Java binding library is Jackson One popular XML-to-Java binding library is XStream

If you intend to use this in a web application, maybe you would like to consider Spring 3 MVC's facilities for this. Through annotations it does the conversion automatically and you can tell it whether you want XML or JSON (or various other formats). This might be the common interface you are looking for too.

jbx
  • 21,365
  • 18
  • 90
  • 144