JUCE icon indicating copy to clipboard operation
JUCE copied to clipboard

JSON: Add toString() method for StringPairArray

Open daleonov opened this issue 3 years ago • 0 comments

Had a few StringPairArray's I had to convert to JSON, was surprised JUCE can't do that yet. I used JSONFormatter stuff as much as possible, so it should behave nicely. Example:

    juce::StringPairArray data;

    DBG("*** Empty array ***");
    DBG("Single line" << juce::newLine << juce::JSON::toString(data, true));
    DBG("Multiple lines" << juce::newLine << juce::JSON::toString(data, false));

    DBG(juce::newLine << "*** One item ***");
    data.set("Key1", "Value1");
    DBG("Single line" << juce::newLine << juce::JSON::toString(data, true));
    DBG("Multiple lines" << juce::newLine << juce::JSON::toString(data, false));

    DBG(juce::newLine << "*** A bunch of items ***");
    data.set("Key2", "Value2");
    data.set("Key3", "Value\that\needs\escaping");
    DBG("Single line" << juce::newLine << juce::JSON::toString(data, true));
    DBG("Multiple lines" << juce::newLine << juce::JSON::toString(data, false));

Output:

*** Empty array ***
Single line
{}
Multiple lines
{}

*** One item ***
Single line
{"Key1": "Value1"}
Multiple lines
{
  "Key1": "Value1"
}

*** A bunch of items ***
Single line
{"Key1": "Value1", "Key2": "Value2", "Key3": "Value\\that\\needsescaping"}
Multiple lines
{
  "Key1": "Value1",
  "Key2": "Value2",
  "Key3": "Value\\that\\needsescaping"
}

It can also live as StringPairArray method, but I think you prefer to keep all JSON stuff in one class. Also, noticed weird string escaping behaviour (see how one backslash has disappeared in last item?), but that's another matter.

daleonov avatar Jan 07 '22 16:01 daleonov