0

In my current work, we need to serialize some data and use it later. Our data is in this format:

<resources>
<style name="Person">
    <item name="name">name</item>
    <item name="age">32</item>
</style>
<style name="Student" parent="Person">
        <item name="course">course</item>
</style>
</resources>

We need to parse these data frequently.

Can we do something similar in json? Apart from xml and json, is there any other data structure to support inheritance better? And what is the best way to parse this parent-child relationship data?

Update:

I like the idea of @Ipad1gs. I just wanted to clear few more things in xml, we can extend this to and even we can override things like the name.

<resources>
    <style name="Person">
        <item name="name">name</item>
        <item name="age">32</item>
    </style>
    <style name="Student" parent="Person">
            <item name="course">course</item>
    </style>
<style name="Employee" parent="Person">
          <item name="name">employee name</item>
            <item name="department">department</item>
    </style>
    </resources>

How can we do the similar thing in json using composition?

  • since you're doing Java, check the Jackson library, just do your model as you think you should and use it toserialize your data. – Walfrat Jun 8 '18 at 12:06
  • You seem to be assuming there is one instance of Person, while at the same time there is an Employee and a Student. You have at least 2 Person instances, or the object you are serialising is inheriting both (and therefore Person, Employee and Student are interfaces, so the implementing object only needs one value for name) – Caleth Jun 8 '18 at 12:08
4

I suggest not using inheritance, but composition, eg:in json:

Here is a "Person"

{
  "name":"name",
  "age":32
}

Here is a "Student", which holds a "Person" instead of becoming a "Person":

{
    "course":"name"
    "person":{
      "name":"name",
      "age":32
    }
}
  • I like this idea. just wanted to clear few more things I have updated my question could you please check it. – Vivart Jun 8 '18 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.