json2yson.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "json2yson.h"
  2. #include <library/cpp/yson/parser.h>
  3. #include <library/cpp/yson/json/json_writer.h>
  4. #include <library/cpp/yson/json/yson2json_adapter.h>
  5. namespace NJson2Yson {
  6. static void WriteJsonValue(const NJson::TJsonValue& jsonValue, NYT::TYson2JsonCallbacksAdapter* adapter) {
  7. switch (jsonValue.GetType()) {
  8. default:
  9. case NJson::JSON_NULL:
  10. adapter->OnNull();
  11. break;
  12. case NJson::JSON_BOOLEAN:
  13. adapter->OnBoolean(jsonValue.GetBoolean());
  14. break;
  15. case NJson::JSON_DOUBLE:
  16. adapter->OnDouble(jsonValue.GetDouble());
  17. break;
  18. case NJson::JSON_INTEGER:
  19. adapter->OnInteger(jsonValue.GetInteger());
  20. break;
  21. case NJson::JSON_UINTEGER:
  22. adapter->OnUInteger(jsonValue.GetUInteger());
  23. break;
  24. case NJson::JSON_STRING:
  25. adapter->OnString(jsonValue.GetString());
  26. break;
  27. case NJson::JSON_ARRAY: {
  28. adapter->OnOpenArray();
  29. const NJson::TJsonValue::TArray& arr = jsonValue.GetArray();
  30. for (const auto& it : arr)
  31. WriteJsonValue(it, adapter);
  32. adapter->OnCloseArray();
  33. break;
  34. }
  35. case NJson::JSON_MAP: {
  36. adapter->OnOpenMap();
  37. const NJson::TJsonValue::TMapType& map = jsonValue.GetMap();
  38. for (const auto& it : map) {
  39. adapter->OnMapKey(it.first);
  40. WriteJsonValue(it.second, adapter);
  41. }
  42. adapter->OnCloseMap();
  43. break;
  44. }
  45. }
  46. }
  47. void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, NYson::TYsonWriter* ysonWriter) {
  48. NYT::TYson2JsonCallbacksAdapter adapter(ysonWriter);
  49. WriteJsonValue(inputValue, &adapter);
  50. }
  51. void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, IOutputStream* outputStream) {
  52. NYson::TYsonWriter ysonWriter(outputStream, NYson::EYsonFormat::Binary, ::NYson::EYsonType::Node, false);
  53. SerializeJsonValueAsYson(inputValue, &ysonWriter);
  54. }
  55. void SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue, TString& result) {
  56. TStringOutput resultStream(result);
  57. SerializeJsonValueAsYson(inputValue, &resultStream);
  58. }
  59. TString SerializeJsonValueAsYson(const NJson::TJsonValue& inputValue) {
  60. TString result;
  61. SerializeJsonValueAsYson(inputValue, result);
  62. return result;
  63. }
  64. bool DeserializeYsonAsJsonValue(IInputStream* inputStream, NJson::TJsonValue* outputValue, bool throwOnError) {
  65. NJson::TParserCallbacks parser(*outputValue);
  66. NJson2Yson::TJsonBuilder consumer(&parser);
  67. NYson::TYsonParser ysonParser(&consumer, inputStream, ::NYson::EYsonType::Node);
  68. try {
  69. ysonParser.Parse();
  70. } catch (...) {
  71. if (throwOnError) {
  72. throw;
  73. }
  74. return false;
  75. }
  76. return true;
  77. }
  78. bool DeserializeYsonAsJsonValue(TStringBuf str, NJson::TJsonValue* outputValue, bool throwOnError) {
  79. TMemoryInput inputStream(str);
  80. return DeserializeYsonAsJsonValue(&inputStream, outputValue, throwOnError);
  81. }
  82. void ConvertYson2Json(IInputStream* inputStream, IOutputStream* outputStream) {
  83. NYT::TJsonWriter writer(outputStream, ::NYson::EYsonType::Node, NYT::JF_TEXT, NYT::JAM_ON_DEMAND, NYT::SBF_BOOLEAN);
  84. NYson::TYsonParser ysonParser(&writer, inputStream, ::NYson::EYsonType::Node);
  85. ysonParser.Parse();
  86. }
  87. void ConvertYson2Json(TStringBuf yson, IOutputStream* outputStream) {
  88. TMemoryInput inputStream(yson);
  89. ConvertYson2Json(&inputStream, outputStream);
  90. }
  91. TString ConvertYson2Json(TStringBuf yson) {
  92. TString json;
  93. TStringOutput outputStream(json);
  94. ConvertYson2Json(yson, &outputStream);
  95. return json;
  96. }
  97. }