json_prettifier.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "json_reader.h"
  3. #include <util/generic/ylimits.h>
  4. namespace NJson {
  5. struct TJsonPrettifier {
  6. bool Unquote = false;
  7. ui8 Padding = 4;
  8. bool SingleQuotes = false;
  9. bool Compactify = false;
  10. bool Strict = false;
  11. bool NewUnquote = false; // use new unquote, may break old tests
  12. ui32 MaxPaddingLevel = Max<ui32>();
  13. static TJsonPrettifier Prettifier(bool unquote = false, ui8 padding = 4, bool singlequotes = false) {
  14. TJsonPrettifier p;
  15. p.Unquote = unquote;
  16. p.Padding = padding;
  17. p.SingleQuotes = singlequotes;
  18. return p;
  19. }
  20. static TJsonPrettifier Compactifier(bool unquote = false, bool singlequote = false) {
  21. TJsonPrettifier p;
  22. p.Unquote = unquote;
  23. p.Padding = 0;
  24. p.Compactify = true;
  25. p.SingleQuotes = singlequote;
  26. return p;
  27. }
  28. bool Prettify(TStringBuf in, IOutputStream& out) const;
  29. TString Prettify(TStringBuf in) const;
  30. static bool MayUnquoteNew(TStringBuf in);
  31. static bool MayUnquoteOld(TStringBuf in);
  32. };
  33. inline TString PrettifyJson(TStringBuf in, bool unquote = false, ui8 padding = 4, bool sq = false) {
  34. return TJsonPrettifier::Prettifier(unquote, padding, sq).Prettify(in);
  35. }
  36. inline bool PrettifyJson(TStringBuf in, IOutputStream& out, bool unquote = false, ui8 padding = 4, bool sq = false) {
  37. return TJsonPrettifier::Prettifier(unquote, padding, sq).Prettify(in, out);
  38. }
  39. inline bool CompactifyJson(TStringBuf in, IOutputStream& out, bool unquote = false, bool sq = false) {
  40. return TJsonPrettifier::Compactifier(unquote, sq).Prettify(in, out);
  41. }
  42. inline TString CompactifyJson(TStringBuf in, bool unquote = false, bool sq = false) {
  43. return TJsonPrettifier::Compactifier(unquote, sq).Prettify(in);
  44. }
  45. }