0

I'd like to serialize and deserialize objects with arbitrary fields. I've tried to have the object with the arbitrary fields extend Dictionary<string, object> in hopes that I would be able to set the arbitrary fields as Dictionary entries. In this case I expect to have Company and Position in the json response (listed in code comments) in addition to the manager and office fields. Unfortunately I'm able get the arbitrary fields but unable to get the non arbitrary fields.

I would also like to be able to add arbitrary objects, not just strings (ie add a salary object with base salary, bonus, etc to the job). I also have some restrictions and cannot use dynamic for this.

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Job Job { get; set; }
}

public class Job : Dictionary<string, object>
{
    public string Company { get; set; }
    public string Position { get; set; }
}

var job = new Job()
{
    Company = "Super Mart",
    Position = "Cashier"
};

// Set arbitrary fields
job["manager"] = "Kathy";
job["office"] = "001";

var john = new Person()
{
    Name = "John Doe",
    Age = 41,
    Job = job
};

var employeeJson = JsonConvert.SerializeObject(john, Formatting.Indented);
Log.Debug("TestSerialization", "json: {0}", employeeJson);
// Result
// {
//   "Name": "John Doe",
//   "Age": 41,
//   "Job": {
//     "manager": "Kathy",
//     "office": "001"
//   }
// }

var johnDoe = JsonConvert.DeserializeObject<Person>(employeeJson);
Log.Debug("TestSerialization", "name: {0}, age: {1}", johnDoe.Name, johnDoe.Age);
// Result
// name: John Doe, age: 41

Log.Debug("TestSerialization", "company: {0}, position: {1}", johnDoe.Job.Company, johnDoe.Job.Position);
// Result
// company: , position:

Log.Debug("TestSerialization", "manager: {0}, office: {1}", johnDoe.Job["manager"], johnDoe.Job["office"]);
// Result
// manager: Kathy, office: 001

My result json from deserialization using this code is

{
  "Name": "John Doe",
  "Age": 41,
  "Job": {
    "manager": "Kathy",
    "office": "001"
  }
}

I would like the result json to be (what the service would expect)

{
  "Name": "John Doe",
  "Age": 41,
  "Job": {
    "Company" = "Super Mart",
    "Position" = "Cashier"
    "manager": "Kathy",
    "office": "001"
  }
}
5
  • It’s not clear what result you get and what result you expect. Can you provide this information? Commented Feb 28, 2019 at 22:54
  • The results are listed in comments in the code block. What I would like to get is company and position - the two non arbitrary fields in the Job model to not be null. Commented Feb 28, 2019 at 23:08
  • You I probably are looking for custom contract resolvers. Commented Feb 28, 2019 at 23:57
  • The results are listed, but it's not clear, what result you expect. Please modify your question by adding final json version, that you want to get. Commented Mar 1, 2019 at 10:09
  • I apologize - I added explicit results and expectation to the question Commented Mar 1, 2019 at 13:26

1 Answer 1

1

I think the problem with your job Class, it derived from a dictionary so when you serialize it will not consider its members. only dictionary values,

Try this way, I am not sure this will help your context

public class Job 
{
    public string Company { get; set; }
    public string Position { get; set; }
    public Dictionary<string,object> job { get; set; }

    public Job()
    {
        job = new Dictionary<string, object>();
    }
}

Debug.Log(johnDoe.Job.job["manager"]+"-"+ johnDoe.Job.job["office"]);

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this wouldn't work in my case. The Job class needs to have explicit Company and Position properties while at the same time accept arbitrary properties. I added an example of what the server would expect in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.