Skip to content

Can without default constructor function for user defined classes when only to_json is needed? #1629

@akeyliu

Description

@akeyliu
  • Describe what you want to achieve.
  1. In my project with many user defined classes, JSON is used to streamlize for communication and log;
  2. I added only to_json() for user defined classes;
  3. I need to add default constructor function for all user defined classes, and it's work.
  4. Some user defined classes with default constructor function is meansless and useless.
    My Questions is if we can ignore default constructor function when we only need to_json()?
  • Describe what you tried.
  1. In to_json() for user defined classes, an example code for CClass is like these:
    friend inline void (JSON& j, const CClass1& obj1) {
    j["name1"] = obj1.strName;
    j["member"] = JSON(obj1.m_obj2);
    };
  2. In upper code, JSON is typedef nlhomann::json, obj1 is the current class object, obj2 is member class object, and maybe is STL object, i.e. vector;
  3. If we have all to_json() for every user defined classes, it's possiable for Arbitrary types conversions without default constructor function for those classes?
  • Describe which system (OS, compiler) you are using.
    Ubuntu 18.04 LTS gcc 7.3

  • Describe which version of the library you are using (release version, develop branch).
    master branch version 3.6.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions