| 1 | // |
| 2 | // Stringifier.cpp |
| 3 | // |
| 4 | // Library: JSON |
| 5 | // Package: JSON |
| 6 | // Module: Stringifier |
| 7 | // |
| 8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/JSON/Stringifier.h" |
| 16 | #include "Poco/JSON/Array.h" |
| 17 | #include "Poco/JSON/Object.h" |
| 18 | #include "Poco/JSONString.h" |
| 19 | #include <iomanip> |
| 20 | |
| 21 | |
| 22 | using Poco::Dynamic::Var; |
| 23 | |
| 24 | |
| 25 | namespace Poco { |
| 26 | namespace JSON { |
| 27 | |
| 28 | |
| 29 | void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent, int step) |
| 30 | { |
| 31 | if (step == -1) step = indent; |
| 32 | |
| 33 | if (any.type() == typeid(Object)) |
| 34 | { |
| 35 | const Object& o = any.extract<Object>(); |
| 36 | o.stringify(out, indent == 0 ? 0 : indent, step); |
| 37 | } |
| 38 | else if (any.type() == typeid(Array)) |
| 39 | { |
| 40 | const Array& a = any.extract<Array>(); |
| 41 | a.stringify(out, indent == 0 ? 0 : indent, step); |
| 42 | } |
| 43 | else if (any.type() == typeid(Object::Ptr)) |
| 44 | { |
| 45 | const Object::Ptr& o = any.extract<Object::Ptr>(); |
| 46 | o->stringify(out, indent == 0 ? 0 : indent, step); |
| 47 | } |
| 48 | else if (any.type() == typeid(Array::Ptr)) |
| 49 | { |
| 50 | const Array::Ptr& a = any.extract<Array::Ptr>(); |
| 51 | a->stringify(out, indent == 0 ? 0 : indent, step); |
| 52 | } |
| 53 | else if (any.isEmpty()) |
| 54 | { |
| 55 | out << "null" ; |
| 56 | } |
| 57 | else if (any.isNumeric() || any.isBoolean()) |
| 58 | { |
| 59 | std::string value = any.convert<std::string>(); |
| 60 | if (any.type() == typeid(char)) formatString(value, out); |
| 61 | else out << value; |
| 62 | } |
| 63 | else if (any.isString() || any.isDateTime() || any.isDate() || any.isTime()) |
| 64 | { |
| 65 | std::string value = any.convert<std::string>(); |
| 66 | formatString(value, out); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | out << any.convert<std::string>(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void Stringifier::formatString(const std::string& value, std::ostream& out) |
| 76 | { |
| 77 | Poco::toJSON(value, out); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | } } // namespace Poco::JSON |
| 82 | |