22 #include <boost/property_tree/json_parser.hpp>
24 #include <com/sun/star/beans/NamedValue.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/reflection/XIdlField.hpp>
28 #include <com/sun/star/reflection/theCoreReflection.hpp>
39 uno::Any jsonToUnoAny(
const boost::property_tree::ptree& aTree)
44 uno::Reference<reflection::XIdlField> aField;
45 boost::property_tree::ptree aNodeNull, aNodeValue, aNodeField;
46 const std::string& rType = aTree.get<std::string>(
"type",
"");
47 const std::string& rValue = aTree.get<std::string>(
"value",
"");
48 uno::Sequence<uno::Reference<reflection::XIdlField>> aFields;
49 uno::Reference<reflection::XIdlClass> xIdlClass
51 ->forName(OUString::fromUtf8(rType.c_str()));
54 uno::TypeClass aTypeClass = xIdlClass->getTypeClass();
55 xIdlClass->createObject(aAny);
56 aFields = xIdlClass->getFields();
57 nFields = aFields.getLength();
58 aNodeValue = aTree.get_child(
"value", aNodeNull);
59 if (nFields > 0 && aNodeValue != aNodeNull)
61 for (sal_Int32 itField = 0; itField < nFields; ++itField)
63 aField = aFields[itField];
64 aNodeField = aNodeValue.get_child(aField->getName().toUtf8().getStr(), aNodeNull);
65 if (aNodeField != aNodeNull)
67 aValue = jsonToUnoAny(aNodeField);
68 aField->set(aAny, aValue);
72 else if (!rValue.empty())
74 if (aTypeClass == uno::TypeClass_VOID)
76 else if (aTypeClass == uno::TypeClass_BYTE)
78 else if (aTypeClass == uno::TypeClass_BOOLEAN)
79 aAny <<= OString(rValue.c_str()).toBoolean();
80 else if (aTypeClass == uno::TypeClass_SHORT)
82 else if (aTypeClass == uno::TypeClass_UNSIGNED_SHORT)
84 else if (aTypeClass == uno::TypeClass_LONG)
86 else if (aTypeClass == uno::TypeClass_UNSIGNED_LONG)
88 else if (aTypeClass == uno::TypeClass_FLOAT)
89 aAny <<= OString(rValue.c_str()).toFloat();
90 else if (aTypeClass == uno::TypeClass_DOUBLE)
92 else if (aTypeClass == uno::TypeClass_STRING)
93 aAny <<= OUString::fromUtf8(rValue.c_str());
102 SequenceAsHashMap::SequenceAsHashMap()
106 SequenceAsHashMap::SequenceAsHashMap(
const css::uno::Any& aSource)
112 SequenceAsHashMap::SequenceAsHashMap(
const css::uno::Sequence< css::uno::Any >& lSource)
117 SequenceAsHashMap::SequenceAsHashMap(
const css::uno::Sequence< css::beans::PropertyValue >& lSource)
122 SequenceAsHashMap::SequenceAsHashMap(
const css::uno::Sequence< css::beans::NamedValue >& lSource)
130 if (!aSource.hasValue())
136 css::uno::Sequence< css::beans::NamedValue > lN;
143 css::uno::Sequence< css::beans::PropertyValue > lP;
150 throw css::lang::IllegalArgumentException(
151 "Any contains wrong type.", css::uno::Reference<css::uno::XInterface>(),
156 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::uno::Any >& lSource)
158 sal_Int32 c = lSource.getLength();
164 css::beans::PropertyValue lP;
165 if (lSource[i] >>= lP)
168 (lP.Name.isEmpty()) ||
169 (!lP.Value.hasValue())
171 throw css::lang::IllegalArgumentException(
172 "PropertyValue struct contains no useful information.",
173 css::uno::Reference<css::uno::XInterface>(), -1);
174 (*this)[lP.Name] = lP.Value;
178 css::beans::NamedValue lN;
179 if (lSource[i] >>= lN)
182 (lN.Name.isEmpty()) ||
183 (!lN.Value.hasValue())
185 throw css::lang::IllegalArgumentException(
186 "NamedValue struct contains no useful information.",
187 css::uno::Reference<css::uno::XInterface>(), -1);
188 (*this)[lN.Name] = lN.Value;
193 if (lSource[i].hasValue())
194 throw css::lang::IllegalArgumentException(
195 "Any contains wrong type.",
196 css::uno::Reference<css::uno::XInterface>(), -1);
200 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
204 sal_Int32 c = lSource.getLength();
205 const css::beans::PropertyValue* pSource = lSource.getConstArray();
208 for (sal_Int32
i=0;
i<c; ++
i)
209 (*
this)[pSource[
i].Name] = pSource[
i].Value;
212 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource)
216 sal_Int32 c = lSource.getLength();
217 const css::beans::NamedValue* pSource = lSource.getConstArray();
220 for (sal_Int32
i=0;
i<c; ++
i)
221 (*
this)[pSource[
i].Name] = pSource[
i].Value;
226 sal_Int32 c =
static_cast<sal_Int32
>(
size());
227 lDestination.realloc(c);
228 css::beans::PropertyValue* pDestination = lDestination.getArray();
235 pDestination[i].Name = pThis->first.maString;
236 pDestination[i].Value = pThis->second;
243 sal_Int32 c =
static_cast<sal_Int32
>(
size());
244 lDestination.realloc(c);
245 css::beans::NamedValue* pDestination = lDestination.getArray();
252 pDestination[i].Name = pThis->first.maString;
253 pDestination[i].Value = pThis->second;
258 css::uno::Any SequenceAsHashMap::getAsConstAny(
bool bAsPropertyValueList)
const
260 css::uno::Any aDestination;
261 if (bAsPropertyValueList)
262 aDestination <<= getAsConstPropertyValueList();
264 aDestination <<= getAsConstNamedValueList();
268 css::uno::Sequence< css::beans::NamedValue > SequenceAsHashMap::getAsConstNamedValueList()
const
270 css::uno::Sequence< css::beans::NamedValue > lReturn;
275 css::uno::Sequence< css::beans::PropertyValue > SequenceAsHashMap::getAsConstPropertyValueList()
const
277 css::uno::Sequence< css::beans::PropertyValue > lReturn;
284 for (
auto const& elem : rCheck)
286 const OUString& sCheckName = elem.first.maString;
287 const css::uno::Any& aCheckValue = elem.second;
293 const css::uno::Any& aFoundValue = pFound->second;
294 if (aFoundValue != aCheckValue)
303 m_aMap.reserve(std::max(
size(), rUpdate.
size()));
304 for (
auto const& elem : rUpdate.
m_aMap)
306 m_aMap[elem.first] = elem.second;
313 boost::property_tree::ptree aTree, aNodeNull, aNodeValue;
314 std::stringstream aStream(rJson.getStr());
315 boost::property_tree::read_json(aStream, aTree);
317 for (
const auto& rPair : aTree)
319 const std::string& rType = rPair.second.get<std::string>(
"type",
"");
320 const std::string& rValue = rPair.second.get<std::string>(
"value",
"");
322 beans::PropertyValue aValue;
323 aValue.Name = OUString::fromUtf8(rPair.first.c_str());
324 if (rType ==
"string")
325 aValue.
Value <<= OUString::fromUtf8(rValue.c_str());
326 else if (rType ==
"boolean")
327 aValue.Value <<= OString(rValue.c_str()).toBoolean();
328 else if (rType ==
"float")
329 aValue.Value <<= OString(rValue.c_str()).toFloat();
330 else if (rType ==
"long")
332 else if (rType ==
"short")
334 else if (rType ==
"unsigned short")
336 else if (rType ==
"int64")
338 else if (rType ==
"int32")
340 else if (rType ==
"int16")
342 else if (rType ==
"uint64")
343 aValue.Value <<= OString(rValue.c_str()).toUInt64();
344 else if (rType ==
"uint32")
346 else if (rType ==
"uint16")
348 else if (rType ==
"[]byte")
350 aNodeValue = rPair.second.get_child(
"value", aNodeNull);
351 if (aNodeValue != aNodeNull && aNodeValue.size() == 0)
353 uno::Sequence<sal_Int8> aSeqByte(reinterpret_cast<const sal_Int8*>(rValue.c_str()),
355 aValue.Value <<= aSeqByte;
358 else if (rType ==
"[]any")
360 aNodeValue = rPair.second.get_child(
"value", aNodeNull);
361 if (aNodeValue != aNodeNull && !aNodeValue.empty())
363 uno::Sequence<uno::Any>
aSeq(aNodeValue.size());
364 std::transform(aNodeValue.begin(), aNodeValue.end(),
aSeq.getArray(),
365 [](
const auto& rSeqPair) {
return jsonToUnoAny(rSeqPair.second); });
366 aValue.Value <<=
aSeq;
370 SAL_WARN(
"comphelper",
"JsonToPropertyValues: unhandled type '" << rType <<
"'");
371 aArguments.push_back(aValue);
SequenceAsHashMapBase::const_iterator const_iterator
sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix=10)
Sequence< PropertyValue > aArguments
enumrange< T >::Iterator begin(enumrange< T >)
std::vector< css::beans::PropertyValue > JsonToPropertyValues(const OString &rJson)
const css::uno::Reference< css::io::XObjectOutputStream > & operator<<(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream, const css::awt::FontDescriptor &_rFont)
double toDouble(std::u16string_view str)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
enumrange< T >::Iterator end(enumrange< T >)
static PropertyMapEntry const * find(const rtl::Reference< PropertySetInfo > &mxInfo, const OUString &aName) noexcept
const css::uno::Reference< css::io::XObjectInputStream > & operator>>(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream, css::awt::FontDescriptor &_rFont)
sal_uInt32 toUInt32(std::u16string_view str, sal_Int16 radix=10)
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
Sequence< sal_Int8 > aSeq
SequenceAsHashMapBase m_aMap
#define SAL_WARN(area, stream)