2

Currently writing a JSON Importer and creating some POJOs based on the data. I also have a requirement to write an JSON Exporter which take the information in the POJOs and exports a JSON doc.

To me I would like to reuse the the same code if possible. Does such a thing exist where you can reverse the function/method? i.e I could (depending on what I pass it) process it in the correct order and output the desired result, using the same code?

Thanks

6
  • Is your question/problem generally about "reversing a function"? or is it specifically about your JSON issue? If it's specifically about serialising JSON then consider a library such as Gson - github.com/google/gson/blob/master/UserGuide.md Commented Mar 25, 2016 at 10:39
  • @BenCottrell Keep in mind that library/tool recommendations are off-topic, so even if that was the OP's intent then we'd have to close the question instead of answering the on-topic part.
    – Ixrec
    Commented Mar 25, 2016 at 10:42
  • I think I as well as maybe others are confused - it sounds as though you wish to create a method that can take a Java object and give you a JSON String, or take a JSON String and give you an Object - but by reverse you mean avoid writing a serializer and deserializer separately? It may be possible - I can't personally think of a way but I'd certainly advise against it - as well as advise against not using GSON or something similar. Commented Mar 25, 2016 at 10:42
  • No it's about "reversing a function", I mentioned the JSON just to put things into context
    – NUIG2014
    Commented Mar 25, 2016 at 10:43
  • 1
    @Ixrec I believe it needs clarifying - the question about reversible functions, while definitely a good/interesting question for the site, may just be a seriously over-complex solution to a very easy/simple problem. Commented Mar 25, 2016 at 10:44

2 Answers 2

8

Yes, this is definitely possible.

The paper also points to other approaches based on

0

I can think of three major reasons why it is generally not possible to programmatically invert an arbitrary function in most general programming languages:

  1. Only a one-to-one function (aka a "bijection") can be inverted even in principle. That means the function must produce a unique output value for every single possible combination of inputs. In practice most functions are not one-to-one, nor would it make any sense for them to be. For instance, a correct JSON parsing function will produce the same POJO for many different strings.

  2. The function would have to be composed entirely of operations/functions that have known inverses. Even the basic arithmetic operations are not quite invertible (due to overflow, underflow, rounding errors and the division by zero problem), and as far as I know the built-in methods of most languages are largely non-invertible (concatenating strings, accessing array elements, taking substrings, etc are all not one-to-one).

  3. It doesn't make sense to talk about inverting a function which has side effects. What is the opposite of sending data over a network connection, saving data to a hard drive or printing a character to a screen? In some contexts these operations may be undoable, but you can't un-print a character before any characters have been printed, whereas you can decrement an integer without incrementing it first.

However, it is theoretically possible to design a language (or a DSL within your favorite language) that consists solely of strictly invertable operations, in which it would be possible to programmatically invert any function. The links in Jörg's answer cover this far better than I can.

For the purposes of your JSON importer/exporter, it's likely that simply writing two separate code paths would be a lot less work than constructing an invertable DSL with all the string building and object constructing operations needed to do what you want.

1
  • 3
    Your first point is technically correct but useless. While the strings {"a":1,"b":2} and { "b": 2, "\u0061": 1 } aren't string-equal, they are equal in the sense that they JSON-encode the same data structure. With an equality relation json_equals(a, b) and functions encode(obj), decode(json), we'd need json_equals(json, encode(decode(json))) to hold for all JSON documents that can be generated by encode(…), and deep_equals(obj, decode(encode(obj))) for all objects that can be encoded. Or phrased differently: we are dealing large equivalence classes, making bijection feasible.
    – amon
    Commented Mar 25, 2016 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.