SHARE
TWEET

Untitled

a guest Oct 14th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Serializer {
  5. public:
  6.     virtual void BeginArray() {}
  7.     virtual void EndArray() {}
  8.     virtual void AddArrayItem(const std::string&) {}
  9.     virtual ~Serializer() {}
  10. };
  11. class JsonSerializer : public Serializer {
  12. public:
  13.     bool start = 0;
  14.     bool closelast = 0;
  15.     void BeginArray() override {
  16.         if (closelast)
  17.             std::cout << ',';
  18.         closelast = 0;
  19.         start = 1;
  20.         std::cout << '[';
  21.     }
  22.     void AddArrayItem(const std::string& s) override {
  23.         closelast = 0;
  24.         if (start) {
  25.             start = 0;
  26.             std::cout << '"' << s << '"';
  27.         } else {
  28.             std::cout << ',' << '"' << s << '"';
  29.         }
  30.     }
  31.     void EndArray() override {
  32.         start = 0;
  33.         std::cout << ']';
  34.         closelast = 1;
  35.     }
  36. };
  37. //    int main() {
  38. //        using namespace std;
  39. //        JsonSerializer a;
  40. //        string s;
  41. //        while (cin >> s) {
  42. //            if (s[0] == 'B')
  43. //                a.BeginArray();
  44. //            if (s[0] == 'E')
  45. //                a.EndArray();
  46. //            if (s[0] == 'A') {
  47. //                string s1;
  48. //                cin >> s1;
  49. //                a.AddArrayItem(s1);
  50. //            }
  51. //        }
  52. //    }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top