Bug #76216 Version of rapidjson in extra/ directory is out of date and buggy
Submitted: 9 Mar 2015 3:51 Modified: 13 Apr 2015 12:03
Reporter: Justin Swanhart Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: Packaging Severity:S2 (Serious)
Version:5.7.5 OS:Any
Assigned to: Assigned Account CPU Architecture:Any

[9 Mar 2015 3:51] Justin Swanhart
Description:
I tried using the rapidjson that is bundled with 5.7.5 but was not successful in doing so because the SAX style parser included in the bundled library identifies array and object keys as strings instead of keys.

How to repeat:
Use code such as the following:

  struct Handler {
    std::ostringstream oss;
    std::string key;
    bool Null()             { this->oss << this->key << "=" << "NULL" << "\n";  return true; }
    bool Bool(bool b)       { this->oss << this->key << "=" << std::boolalpha << b << "\n" ; return true; }
    bool Int(int i)         { this->oss << this->key << "=" << i << "\n"; return true; }
    bool Uint(unsigned u)   { this->oss << this->key << "=" << u << "\n"; return true; }
    bool Int64(int64_t i)   { this->oss << this->key << "=" << i << "\n"; return true; }
    bool Uint64(uint64_t u) { this->oss << this->key << "=" << u << "\n"; return true; }
    bool Double(double d)   { this->oss << this->key << "=" << d << "\n"; return true; }
    bool String(const char* str, SizeType length, bool copy) { 
        this->oss << this->key << "=" << str << "\n";
        return true;
    }
    bool Key(const char* str, SizeType length, bool copy) { 
        this->key.assign(str, length);
        return true;
    }
    /* these do nothing */
    bool StartObject() { return true; }
    bool EndObject(SizeType memberCount) { return true; }
    bool StartArray() { return true; }
    bool EndArray(SizeType elementCount) { return true; }
  };
 
  Handler handler;
  Reader reader;
  StringStream ss(json.c_str());
  reader.Parse<0>(ss, handler);
 

Suggested fix:
Bundle newer version of the library.
[10 Mar 2015 12:17] Erlend Dahl
Erik, can you please take a look?
[13 Mar 2015 12:03] Erik Frøseth
The version of rapidjson bundled with MySQL does not support the "Key" callback function, so this is not a bug per se (see BaseReaderHandler in reader.h). The callback function for "Key" was introduced in rapidjson a few months after the version bundled with MySQL

It is possible to differentiate between key and value by keeping track of the parser state. For instance, after each StartObject() the first value should always be a key/name. Something like this should get things started:

  struct Handler
  {
    bool expect_key = false;

    ...

    bool String(const char* str, SizeType length, bool copy)
    {
      if (expect_key)
      {
        cout << "Key(" << str << ")" << endl;
        expect_key = false;
      }
      else
      {
        cout << "String(" << str << ")" << endl;
        expect_key = true;
      }
      return true;
    }
    bool StartObject()
    {
      expect_key = true;
      return true;
    }

    ...
  };
[14 Apr 2015 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".