llsdjson.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @file llsdjson.cpp
  3. * @brief LLSD flexible data system
  4. *
  5. * $LicenseInfo:firstyear=2015&license=viewergpl$
  6. *
  7. * Copyright (c) 2015, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llsdjson.h"
  34. LLSD llsd_from_json(const lljson& val)
  35. {
  36. LLSD result;
  37. switch (val.type())
  38. {
  39. case lljson::value_t::null:
  40. default:
  41. break;
  42. case lljson::value_t::boolean:
  43. result = LLSD(val.template get_ref<const lljson::boolean_t&>());
  44. break;
  45. case lljson::value_t::number_integer:
  46. result =
  47. LLSD(val.template get_ref<const lljson::number_integer_t&>());
  48. break;
  49. case lljson::value_t::number_unsigned:
  50. result =
  51. LLSD(val.template get_ref<const lljson::number_unsigned_t&>());
  52. break;
  53. case lljson::value_t::number_float:
  54. result =
  55. LLSD(val.template get_ref<const lljson::number_float_t&>());
  56. break;
  57. case lljson::value_t::object:
  58. result = LLSD::emptyMap();
  59. for (lljson::const_iterator it = val.cbegin(), end = val.cend();
  60. it != end; ++it)
  61. {
  62. result[it.key()] = llsd_from_json(it.value());
  63. }
  64. break;
  65. case lljson::value_t::array:
  66. result = LLSD::emptyArray();
  67. for (lljson::const_iterator it = val.cbegin(), end = val.cend();
  68. it != end; ++it)
  69. {
  70. result.append(llsd_from_json(it.value()));
  71. }
  72. break;
  73. case lljson::value_t::string:
  74. result = LLSD(val.template get_ref<const lljson::string_t&>());
  75. break;
  76. }
  77. LL_DEBUGS("Json") << "Converted from:\n" << val << "\nto:\n" << result
  78. << LL_ENDL;
  79. return result;
  80. }
  81. lljson llsd_to_json(const LLSD& val)
  82. {
  83. lljson result;
  84. switch (val.type())
  85. {
  86. case LLSD::TypeUndefined:
  87. // 'result' is already initialized to a null json value. HB
  88. break;
  89. case LLSD::TypeBoolean:
  90. result = val.asBoolean();
  91. break;
  92. case LLSD::TypeInteger:
  93. result = val.asInteger();
  94. break;
  95. case LLSD::TypeReal:
  96. result = val.asReal();
  97. break;
  98. case LLSD::TypeURI:
  99. case LLSD::TypeDate:
  100. case LLSD::TypeUUID:
  101. case LLSD::TypeString:
  102. result = val.asString();
  103. break;
  104. case LLSD::TypeMap:
  105. result = lljson::object();
  106. for (LLSD::map_const_iterator it = val.beginMap(),
  107. end = val.endMap();
  108. it != end; ++it)
  109. {
  110. result[it->first] = llsd_to_json(it->second);
  111. }
  112. break;
  113. case LLSD::TypeArray:
  114. result = lljson::array();
  115. for (LLSD::array_const_iterator it = val.beginArray(),
  116. end = val.endArray();
  117. it != end; ++it)
  118. {
  119. result.emplace_back(llsd_to_json(*it));
  120. }
  121. break;
  122. case LLSD::TypeBinary:
  123. default:
  124. llerrs << "Unsupported conversion to JSON from LLSD type: "
  125. << val.type() << llendl;
  126. }
  127. LL_DEBUGS("Json") << "Converted from:\n" << val << "\nto:\n" << result
  128. << LL_ENDL;
  129. return result;
  130. }