Skip to content

How to deserialize array with derived objects #716

Description

@MattiasEppler

Hi

I need to deserialize a array that have derived objects.

{
 "systems":
  [ 
     {"type": "system1", "name": "system_1"  special_param_1 : 1 },
     {"type": "system2", "name": "system_2", special_param_2 : "Value2" },
     {"type": "system3", "name": "system_3", special_param_3 : 3.0, special_param_3_1 : "Value_3"  }
  ]
}

so in my from_json I have to do what? Sorry but I do not realy know how to solve this.
I started with:

void from_json(cons json& j, settings& settings)
{
   settings.systems = j.at("systems").get<vector<systembase>>();
   ??
}

void from_json(cons json& j, systembase& base)
{
  base.type =j.at["type"].get<string>();
  base.name =j.at["name"].get<string>();

  switch (base.type)
  {
   case :
   ...? 
  }
}

void from_json(cons json& j, systemderived_1& derived)
{
 derived = j.at["special_param_1"].get<int>();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: questionsolution: proposed fixa fix for the issue has been proposed and waits for confirmationstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions