diff --git a/modules/juce_core/containers/juce_Variant.cpp b/modules/juce_core/containers/juce_Variant.cpp index 05e6d929d3..1426f43f2d 100644 --- a/modules/juce_core/containers/juce_Variant.cpp +++ b/modules/juce_core/containers/juce_Variant.cpp @@ -175,7 +175,7 @@ public: int toInt (const ValueUnion& data) const noexcept override { return (int) data.doubleValue; } int64 toInt64 (const ValueUnion& data) const noexcept override { return (int64) data.doubleValue; } double toDouble (const ValueUnion& data) const noexcept override { return data.doubleValue; } - String toString (const ValueUnion& data) const override { return String (data.doubleValue, 20); } + String toString (const ValueUnion& data) const override { return minimiseLengthOfFloatString (String (data.doubleValue, 15, true)); } bool toBool (const ValueUnion& data) const noexcept override { return data.doubleValue != 0.0; } bool isDouble() const noexcept override { return true; } bool isComparable() const noexcept override { return true; } diff --git a/modules/juce_core/javascript/juce_JSON.h b/modules/juce_core/javascript/juce_JSON.h index b7abed2e07..ff1588b9b3 100644 --- a/modules/juce_core/javascript/juce_JSON.h +++ b/modules/juce_core/javascript/juce_JSON.h @@ -110,13 +110,14 @@ public: /** Writes a JSON-formatted representation of the var object to the given stream. If allOnOneLine is true, the result will be compacted into a single line of text with no carriage-returns. If false, it will be laid-out in a more human-readable format. - The maximumDecimalPlaces parameter determines the precision of floating point numbers. + The maximumDecimalPlaces parameter determines the precision of floating point numbers + in scientific notation. @see toString */ static void writeToStream (OutputStream& output, const var& objectToFormat, bool allOnOneLine = false, - int maximumDecimalPlaces = 20); + int maximumDecimalPlaces = 15); /** Returns a version of a string with any extended characters escaped. */ static String escapeString (StringRef); diff --git a/modules/juce_core/juce_core.cpp b/modules/juce_core/juce_core.cpp index 72723c39ba..2e7c37c63d 100644 --- a/modules/juce_core/juce_core.cpp +++ b/modules/juce_core/juce_core.cpp @@ -125,7 +125,6 @@ #include "containers/juce_PropertySet.cpp" #include "containers/juce_ReferenceCountedArray.cpp" #include "containers/juce_SparseSet.cpp" -#include "containers/juce_Variant.cpp" #include "files/juce_DirectoryIterator.cpp" #include "files/juce_File.cpp" #include "files/juce_FileInputStream.cpp" @@ -172,6 +171,7 @@ #include "time/juce_RelativeTime.cpp" #include "time/juce_Time.cpp" #include "unit_tests/juce_UnitTest.cpp" +#include "containers/juce_Variant.cpp" #include "javascript/juce_JSON.cpp" #include "javascript/juce_Javascript.cpp" #include "containers/juce_DynamicObject.cpp" diff --git a/modules/juce_data_structures/values/juce_ValueTree.cpp b/modules/juce_data_structures/values/juce_ValueTree.cpp index a4ffc0062a..8ff2983e9c 100644 --- a/modules/juce_data_structures/values/juce_ValueTree.cpp +++ b/modules/juce_data_structures/values/juce_ValueTree.cpp @@ -1157,32 +1157,63 @@ public: void runTest() override { - beginTest ("ValueTree"); - auto r = getRandom(); - - for (int i = 10; --i >= 0;) { - MemoryOutputStream mo; - auto v1 = createRandomTree (nullptr, 0, r); - v1.writeToStream (mo); + beginTest ("ValueTree"); - MemoryInputStream mi (mo.getData(), mo.getDataSize(), false); - auto v2 = ValueTree::readFromStream (mi); - expect (v1.isEquivalentTo (v2)); + auto r = getRandom(); - MemoryOutputStream zipped; + for (int i = 10; --i >= 0;) { - GZIPCompressorOutputStream zippedOut (zipped); - v1.writeToStream (zippedOut); - } - expect (v1.isEquivalentTo (ValueTree::readFromGZIPData (zipped.getData(), zipped.getDataSize()))); + MemoryOutputStream mo; + auto v1 = createRandomTree (nullptr, 0, r); + v1.writeToStream (mo); + + MemoryInputStream mi (mo.getData(), mo.getDataSize(), false); + auto v2 = ValueTree::readFromStream (mi); + expect (v1.isEquivalentTo (v2)); + + MemoryOutputStream zipped; + { + GZIPCompressorOutputStream zippedOut (zipped); + v1.writeToStream (zippedOut); + } + expect (v1.isEquivalentTo (ValueTree::readFromGZIPData (zipped.getData(), zipped.getDataSize()))); - std::unique_ptr xml1 (v1.createXml()); - std::unique_ptr xml2 (v2.createCopy().createXml()); - expect (xml1->isEquivalentTo (xml2.get(), false)); + std::unique_ptr xml1 (v1.createXml()); + std::unique_ptr xml2 (v2.createCopy().createXml()); + expect (xml1->isEquivalentTo (xml2.get(), false)); - auto v4 = v2.createCopy(); - expect (v1.isEquivalentTo (v4)); + auto v4 = v2.createCopy(); + expect (v1.isEquivalentTo (v4)); + } + } + + { + beginTest ("Float formatting"); + + ValueTree testVT ("Test"); + Identifier number ("number"); + + std::map tests; + tests[1] = "1"; + tests[1.1] = "1.1"; + tests[1.01] = "1.01"; + tests[0.76378] = "7.6378e-1"; + tests[-10] = "-1e1"; + tests[10.01] = "1.001e1"; + tests[0.0123] = "1.23e-2"; + tests[-3.7e-27] = "-3.7e-27"; + tests[1e+40] = "1e40"; + + for (auto& test : tests) + { + testVT.setProperty (number, test.first, nullptr); + auto lines = StringArray::fromLines (testVT.toXmlString()); + lines.removeEmptyStrings(); + auto numLines = lines.size(); + expect (numLines > 1); + expectEquals (lines[numLines - 1], ""); + } } } };