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?

asked Jun 8, 2018 at 3:40
2
  • since you're doing Java, check the Jackson library, just do your model as you think you should and use it toserialize your data. Commented Jun 8, 2018 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) Commented Jun 8, 2018 at 12:08

1 Answer 1

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
 }
}
answered Jun 8, 2018 at 5:37
1
  • I like this idea. just wanted to clear few more things I have updated my question could you please check it. Commented Jun 8, 2018 at 9:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.