JSON 在 WinCE 下的使用(1)

时间:2021-03-20 17:27:32

使用的 JSON 版本是:rapidjson-v1.1.0-13-g5268211,先来看格式化成字符串的功能实现。

使用 VS2008 的对话框工程模板建立的新工程,将如下代码放一个 .cpp 文件中,然后增加到工程中,最后在窗体初始化中调用函数 TestPrittyWriter 就可以得到想要的结果。

如下代码:


#include "stdafx.h"

/*
 * 功能: 将结构化(类)存贮的信息格式化输出 JSon 字符串,格式化后的结果见最后所附。
 * 代码有部分注释,以方便理解功能。
*/

#include <vector>
#include "rapidjson/prettywriter.h" // for stringify JSON

// 附加包含目录: ..\include
using namespace rapidjson;

class Person
{
public:
  Person(const std::string& name, unsigned age) : name_(name), age_(age)
  {}
  Person(const Person& rhs) : name_(rhs.name_), age_(rhs.age_)
  {}
  virtual ~Person();

  Person& operator=(const Person& rhs)
  {
    name_ = rhs.name_;
    age_ = rhs.age_;
    return *this;
  }

protected:
  template <typename Writer>
  void Serialize(Writer& writer) const
  {
    // This base class just write out name-value pairs, without wrapping within an object.
    writer.String("name");
#if RAPIDJSON_HAS_STDSTRING
    writer.String(name_);
#else
    writer.String(name_.c_str(), static_cast<SizeType>(name_.length())); // Supplying length of string is faster.
#endif
    writer.String("age");
    writer.Uint(age_);
  }

private:
  std::string name_;
  unsigned age_;
};

Person::~Person()
{
}

//////////////////////////////////////////////////////////////////////////
class Education
{
public:
  Education(const std::string& school, double GPA) : school_(school), GPA_(GPA)
  {}
  Education(const Education& rhs) : school_(rhs.school_), GPA_(rhs.GPA_)
  {}

  template <typename Writer>
  void Serialize(Writer& writer) const
  {
    writer.StartObject();

    writer.String("school");
#if RAPIDJSON_HAS_STDSTRING
    writer.String(school_);
#else
    writer.String(school_.c_str(), static_cast<SizeType>(school_.length()));
#endif

    writer.String("GPA");
    writer.Double(GPA_);

    writer.EndObject();
  }

private:
  std::string school_;
  double GPA_;
};

//////////////////////////////////////////////////////////////////////////
class Dependent : public Person
{
public:
  Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education)
  {}
  Dependent(const Dependent& rhs) : Person(rhs), education_(0)
  {
    education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_);
  }
  virtual ~Dependent();

  Dependent& operator=(const Dependent& rhs)
  {
    if (this == &rhs)
      return *this;
    delete education_;
    education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_);
    return *this;
  }

  template <typename Writer>
  void Serialize(Writer& writer) const
  {
    writer.StartObject();

    Person::Serialize(writer);    // bool WriteStartObject() { os_->Put('{'); return true; }

    writer.String("education");
    if (education_)
      education_->Serialize(writer);
    else
      writer.Null();

    writer.EndObject();       // bool WriteEndObject()   { os_->Put('}'); return true; }
  }

private:
  Education *education_;
};

Dependent::~Dependent()
{
  delete education_; 
}

//////////////////////////////////////////////////////////////////////////
class Employee : public Person
{
public:
  Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married)
  {}
  Employee(const Employee& rhs) : Person(rhs), dependents_(rhs.dependents_), married_(rhs.married_)
  {}
  virtual ~Employee();

  Employee& operator=(const Employee& rhs)
  {
    static_cast<Person&>(*this) = rhs;
    dependents_ = rhs.dependents_;
    married_ = rhs.married_;
    return *this;
  }

  void AddDependent(const Dependent& dependent)
  {
    dependents_.push_back(dependent);
  }

  template <typename Writer>
  void Serialize(Writer& writer) const
  {
    writer.StartObject();   // bool WriteStartObject() { os_->Put('{'); return true; }

    Person::Serialize(writer);

    writer.String("married");
    writer.Bool(married_);

    writer.String(("dependents"));
    writer.StartArray();      //  bool WriteStartArray()  { os_->Put('['); return true; }
    for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
      dependentItr->Serialize(writer);
    writer.EndArray();        // bool WriteEndArray()    { os_->Put(']'); return true; }

    writer.EndObject();     // bool WriteEndObject()   { os_->Put('}'); return true; }
  }

private:
  std::vector<Dependent> dependents_;
  bool married_;
};

Employee::~Employee()
{
}

//////////////////////////////////////////////////////////////////////////
void TestPrittyWriter(void)
{
  std::vector<Employee> employees;

  employees.push_back(Employee("Milo YIP", 34, true));
  employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5)));
  employees.back().AddDependent(Dependent("Mio YIP", 1));

  employees.push_back(Employee("Percy TSE", 30, false));

  StringBuffer sb;
  PrettyWriter<StringBuffer> writer(sb);

  writer.StartArray();
  for(std::vector<Employee>::const_iterator employeeItr = employees.begin();employeeItr != employees.end();++employeeItr)
    employeeItr->Serialize(writer);
  writer.EndArray();

  // puts(sb.GetString());
  // RETAILMSG(1,(L"Result: %s\r\n",sb.GetString()));   // 乱码
  printf("%s",sb.GetString());
}
/*
 **************************运行结果输出************************************
[
  {
    "name": "Milo YIP",
    "age": 34,
    "married": true,
    "dependents": [
      {
        "name": "Lua YIP",
        "age": 3,
        "education": {
          "school": "Happy Kindergarten",
          "GPA": 3.5
        }
      },
      {
        "name": "Mio YIP",
        "age": 1,
        "education": null
      }
    ]
  },
  {
    "name": "Percy TSE",
    "age": 30,
    "married": false,
    "dependents": []
  }
]
*/