a simple-ish json question

20 views
Skip to first unread message

Johnny TwoShoes

unread,
Jan 8, 2016, 10:31:51 AM1/8/16
to Realm Java
Hello,

If I have two sets of data:

{"event_id":"20872","event_action":"update","event_data”:[{“a”:”1”,”b”:”1”}],"event_type”:”typea”}


{"event_id":"20873”,”event_action":"update","event_data”:[{“x”:”1”,”y”:”1”,,”z”:”1”}],”event_type”:”typeb”}



Where type a and type b, are unrelated, how do I model this in realm?


kind regards

Kenneth Geisshirt

unread,
Jan 8, 2016, 11:44:26 AM1/8/16
to Johnny Twoshoes, realm...@googlegroups.com
Hey Johnny,

The model would be simple without the "type a and type b are unrelated". You will probably have to to:

class Event extends RealmObject {
private long event_id;
private String event_action;
private RealmList<EventData> event_data;
private EventType event_type;
}

class EventData extends RealmObject {
private String key;
private long value;
}

class EventType extends RealmObject {
private boolean typea_or_typeb;
private TypeA typea;
private TypeB typeb;
}

class TypeA extends RealmObject {
// fields
}

class TypeB extends RealmObject {
// fields
}

Best,
Kenneth

--
Kenneth Geisshirt
Member of technical staff
{#HS:159019920-2623#}
--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/b0089751-80d1-4cd3-858b-7f3bac4ff603%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Johnny TwoShoes

unread,
Jan 8, 2016, 12:20:27 PM1/8/16
to Kenneth Geisshirt, realm...@googlegroups.com
Hi Kenneth,

Thanks. What I was hoping for, was, if I could put something in the Event constructor, so that I could either json sets of data, through, gson, and it will automatically populate the objects?
Is that possible?

kind regards

Kenneth Geisshirt

unread,
Jan 11, 2016, 5:56:50 AM1/11/16
to Johnny Twoshoes, realm...@googlegroups.com
Hi Johnny,

You can change the Event class to


class Event extends RealmObject {
private long event_id;
private String event_action;
private RealmList<EventData> event_data;
private boolean typea_or_typeb;
private TypeA typea;
private TypeB typeb;
}

and populate the object first as a plain Java object and use Realm.copyToRealm().

Said that, we are exploring various option on how to implement inheritance which might be useful in your case. See https://github.com/realm/realm-java/issues/761.


Best,
Kenneth

--
Kenneth Geisshirt
Member of technical staff
{#HS:159019920-2623#}

Johnny TwoShoes

unread,
Jan 11, 2016, 6:38:05 AM1/11/16
to Kenneth Geisshirt, realm...@googlegroups.com
Hi,

Thanks for your advice. I managed to get it to do what I wanted , eg a simple interface to convert the json into pojo's without extra coding, but, I resorted to a quick small regex hack, to reformat the json string, before converting it. It does the job, but, there is probably a more elegant solution out there.

So:
realm.createObjectFromJson(EventsOut.class, convertStr(loadJSONFromAsset(filename))); //works for all values

and model EventsOut has:
    private RealmList<TypeA> typeas;
    private RealmList<TypeB> typebs;

So, the hack is in the convertStr, which uses regex, to reshuffle the value of type, and replace the EventsData text with it.
So,
{"event_id":"20872","event_action":"update","event_data”:[{“a”:”1”,”b”:”1”}],"event_type”:”typea”}
becomes
{"event_id":"20872","event_action":"update","typea”:[{“a”:”1”,”b”:”1”}],"event_type”:”typea”} 

and
{"event_id":"20873”,”event_action":"update","event_data”:[{“x”:”1”,”y”:”1”,,”z”:”1”}],”event_type”:”typeb”}
becomes
{"event_id":"20873”,”event_action":"update","typeb”:[{“x”:”1”,”y”:”1”,,”z”:”1”}],”event_type”:”typeb”}

it then works. It's not very elegant, but, it saves alot of if then code, especially, if I have alot of different types.

But, I will look into your solution, maybe it's better.

kind regards




Reply all
Reply to author
Forward
0 new messages