1

Let's say I have a set of objects,

foo f;
bar br;
baz bz;

I also have a string of JSON data,

string JSONstring;

Depending on the object type of the JSON string, I need to transform it into either foo, bar, or baz. Ok, cool, I'll have a method for that.

public object parseJSONToFooBarBaz(string jsonString);

What I want to avoid is writing something like:

map<string, object> topLevelJSON = deserialized json string;

if(map[foo] != null) return new foo(jsonString);
else if(map[bar] != null) return new bar(jsonString);
...
// And the list balloons up and is difficult to maintain

I feel like this is either a good condidate or almost a good candidate for a factory pattern, but something doesn't feel quite right. Is there a simple solution that I'm overlooking, or is a set of conditionals or a switch/case really an OK way to solve this?

2
  • Are you trying to write a "generic" JSON mapper that can convert any JSON object to any object within your application without knowing the object type contained within the JSON?
    – ravibhagw
    Jan 26, 2016 at 22:13
  • Not quite, there is some extra logic involved in the mapping that needs to be coded. For example, the JSON field might be "system_id", and it maps to "foo_id" in the foo object but "external_id" in the bar object, and "system_id" in baz.
    – Aj_76
    Jan 26, 2016 at 23:57

1 Answer 1

4

The simplest thing to do is to make a map<string, string -> object) where string -> object just means a function taking a string and producing an object. If the language you are using doesn't support first-class functions you can use the Strategy pattern. The Factory pattern is essentially just a special case of the Strategy pattern and thus just a round-about way of doing first-class functions (though the term "Factory" communicates intent.)

You could then do something like:

foreach(key in factories.keys()) {
    if(topLevelJSON[key] != null) {
        return factories[key](jsonString);
    }
}

I assume these keys are mutually exclusive (as suggested by your if statements), though you could easily adapt the code to return all the choices or have a list of the factories in priority order.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.