1 | // |
2 | // VarHolder.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Core |
6 | // Module: VarHolder |
7 | // |
8 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Dynamic/VarHolder.h" |
16 | #include "Poco/Dynamic/Var.h" |
17 | #include "Poco/UTF8String.h" |
18 | |
19 | |
20 | namespace Poco { |
21 | namespace Dynamic { |
22 | |
23 | |
24 | VarHolder::VarHolder() |
25 | { |
26 | } |
27 | |
28 | |
29 | VarHolder::~VarHolder() |
30 | { |
31 | } |
32 | |
33 | |
34 | namespace Impl { |
35 | |
36 | |
37 | void escape(std::string& target, const std::string& source) |
38 | { |
39 | target = UTF8::escape(source.begin(), source.end()); |
40 | } |
41 | |
42 | |
43 | bool isJSONString(const Var& any) |
44 | { |
45 | return any.type() == typeid(std::string) || |
46 | any.type() == typeid(char) || |
47 | any.type() == typeid(char*) || |
48 | any.type() == typeid(Poco::DateTime) || |
49 | any.type() == typeid(Poco::LocalDateTime) || |
50 | any.type() == typeid(Poco::Timestamp); |
51 | } |
52 | |
53 | |
54 | void appendJSONString(std::string& val, const Var& any) |
55 | { |
56 | std::string json(val); |
57 | val.append(1, '"'); |
58 | escape(json, any.convert<std::string>()); |
59 | val.append(json); |
60 | val.append(1, '"'); |
61 | } |
62 | |
63 | |
64 | void appendJSONKey(std::string& val, const Var& any) |
65 | { |
66 | return appendJSONString(val, any); |
67 | } |
68 | |
69 | |
70 | void appendJSONValue(std::string& val, const Var& any) |
71 | { |
72 | if (any.isEmpty()) |
73 | { |
74 | val.append("null" ); |
75 | } |
76 | else |
77 | { |
78 | bool isStr = isJSONString(any); |
79 | if (isStr) |
80 | { |
81 | appendJSONString(val, any.convert<std::string>()); |
82 | } |
83 | else |
84 | { |
85 | val.append(any.convert<std::string>()); |
86 | } |
87 | } |
88 | } |
89 | |
90 | |
91 | } // namespace Impl |
92 | |
93 | |
94 | } } // namespace Poco::Dynamic |
95 | |