Comments (28)
its not implemented properly for lists. this will be fixed with a future snapshot release
from tikxml.
I'm not on my laptop right now and I'm not sure if it works, but this could work:
class RealmTypeAdapter extends TypeAdapter<RealmList<Foo>>{
@Override
public RealmList<Foo> fromXml(XmlReader reader, TikXmlConfig config) {
// you need to parse some peace a little bit manually
// i.e. each element in the xml stream like <foo bar="123" />
RealmList<Foo> fooList= new RealmList<>();
while(reader.hasElements()) {
reader.beginElement();
String elementName = reader.nextElementName();
TypeAapter<Foo> fooParser = config. getTypeAdapter(Foo.class);
Foo foo = fooParser. fromXml(reader, config);
fooList.add(foo);
// not sure if this one is needed:
reader.endElement();
}
}
@Override
public void toXml(XmlWriter writer, TikXmlConfig config, RealmList<Foo> value, String overridingXmlElementTagName)
throws IOException {
// TODO implement if needed
}
}
Then when instantiating your TikXml instance do something like this:
TikXml tikXml = new TikXml.Builder()
.addTypeAdapter(Types. newParameterizedType(RealmList.class, Foo.class))
.build();
Maybe you also need a compileTimeChecks = false
in your annotation like this: (not sure if required, try it out):
@Xml
class SomeData {
@Element(compileTimeChecks = false)
RealmList<Foo> fooList;
}
from tikxml.
This is also needed for the support of a Map for example.
from tikxml.
With moshi for example you can use something like this Type realmIntegerList = Types.newParameterizedType(RealmInteger.class, RealmList.class);
from tikxml.
And for maps moshi use Type[] keyAndValue = Types.mapKeyAndValueTypes(type, rawType);
from tikxml.
Agreed!
Fabian Terhorst [email protected] schrieb am Do., 2. Juni 2016,
20:16:
And for maps moshi use Type[] keyAndValue =
Types.mapKeyAndValueTypes(type, rawType);β
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#28 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAjnrmc_4n92kTuULUtbzphV8qpO1wYqks5qHw_QgaJpZM4IslHC
.
from tikxml.
Hi, currently trying to use TikXml for RealmList
's. Any known workarounds?
from tikxml.
It's on my TO DO list, but has low priority ...
I'm working right now on other things towards an 1.0
release, but will try to fix this before releasing 1.0 ...
A "workaround" (which for me is a much cleaner approach anyway) is to have XML annotated models and realm models in 2 separated classes.
from tikxml.
I don't think having 2 different entities for the same thing in the data layer is always a cleaner approach (especially if there already is a mapping between data and domain layer) but thanks for the hint.
Too bad this is a actually really nice library (I have always missed an annotation based xml-parsing library since ig-json-parser
). What a pitty
btw the TypeConverter api of LoganSquare is not as nice as moshi's but having the JsonParser
in parse(JsonParser parser)
(equivalent to read(String string)
) can be pretty powerful.
from tikxml.
Ok, so latest 0.5.5-SNAPSHOT
adds pretty the same API as Moshi has (Also a class Types
to create such generic parameterized types).
You can now register TypeAdapter
and TypeConerter
by hand via Type
instead of Class<T>
.
So things likeType[] keyAndValue = Types.mapKeyAndValueTypes(type, rawType);
or RealmList
should work now, but you have to write manually a TypeAdapter
and register that one.
I don't think having 2 different entities for the same thing in the data layer is always a cleaner approach (especially if there already is a mapping between data and domain layer) but thanks for the hint.
Let's talk about that again in one year after a small API change in one of your xml feeds crashes your app because of a missing Realm migration. But I agree, there is no always or recommendation that works for all kind of situations.
from tikxml.
Great feature. π . Awesome work.
from tikxml.
Wow, that's the low priority
speed? Really awesome work!
from tikxml.
One question, how can i prevent the annotation processor to generate a wrong ChildElementBinder that is using new ArrayList?
from tikxml.
So that the annotation processor is using the getTypeAdapter(type) and not getTypeAdapter(class)
from tikxml.
if (value.getObjects() == null) {
value.setObjects(new ArrayList());
}
is the problem
from tikxml.
from tikxml.
apt {
arguments {
" "//this canΒ΄t accept stuff like io.realm.RealmList
}
}
from tikxml.
Indeed, list are a special edge case. I'm rewriting the code generator right now and will consider that ...
from tikxml.
ok thanks.
from tikxml.
@FabianTerhorst could you manage to parse them? I failed.... (even in 0.8.1-SNAPSHOT)
from tikxml.
Any ETA? (rly just looking forward to test this)
from tikxml.
from tikxml.
Yeah, please. A short introduction would be great.
from tikxml.
Hello, i'm trying to use tikxml in conjunction with RealmList, but i'm not sure how to set up the type adapter. Can anyone give me a hand? Thank you :)
from tikxml.
Thank you for the fast response, really appreciated. I'll look into it.
Another question: what if i have N objects which extend RealmObject (the objects are nothing special, they contain just a bunch of strings). Do i need to implement a typeadapter for all of them? or just for realmobject?
Right now if i have something like that:
@Xml
class SomeData {
@Element
Foo foo;
//foo extends RealmObject
}
It just work flawlessly. i feel like i can write a generic typeadapter for RealmLists without creating other typeadapters.
Here's what i got so far:
public class RealmListTypeAdapter implements TypeAdapter<RealmList<? extends RealmObject>> {
@Override
public RealmList<? extends RealmObject> fromXml(XmlReader reader, TikXmlConfig config) throws IOException {
RealmList<? extends RealmObject> list = new RealmList<RealmObject>();
while(reader.hasElement()){
reader.beginElement();
reader.nextElementName();
//and now?
}
return null;
}
Thank you. :)
from tikxml.
public class RealmListTypeAdapter implements TypeAdapter<RealmList<? extends RealmObject>> {
@Override
public RealmList<? extends RealmObject> fromXml(XmlReader reader, TikXmlConfig config) throws IOException {
RealmList<? extends RealmObject> list = new RealmList<RealmObject>();
while(reader.hasElement()){
reader.beginElement();
String elementName = reader.nextElementName();
//and now?
if (elementName.equals(foo)){
TypeAapter<Foo> fooParser = config. getTypeAdapter(Foo.class);
RealmObject foo = fooParser. fromXml(reader, config);
list.add(foo);
} else {
TypeAapter<Bar> barParser = config. getTypeAdapter(Bar.class);
RealmObject bar = fooParser. fromXml(reader, config);
list.add(bar);
}
}
return list;
}
You basically have to make a mapping from "String" (ElementName) to the java class you would like to parse. You can use Map<String, Class>
, then use the returned Class
to get the corresponding TypeAdapter.
Perhaps there will be an easier way to do this in the future (like an abstract class your type adapter has to extend) or a better API i.e. an additional Annotation but I can't make any promise at this time ...
from tikxml.
Hello,
I use the solution you write for RealmList but I have an error :
error: <identifier> expected value.previsions = config.getTypeAdapter(RealmList<Foo>.class).fromXml(reader, config);
Can you help me to resolve this error ?
from tikxml.
Hello, how and where is RealmList
declared?
from tikxml.
Related Issues (20)
- error: incompatible types: Object cannot be converted to RequestBody HOT 1
- How to use NameSpace HOT 1
- How to initiate the lib in kotlin HOT 1
- The Future of TikXML HOT 4
- Dynamic root tag name HOT 6
- XML not generate in defined order HOT 2
- Parsing not work HOT 3
- exception while reading rss feed HOT 7
- Type converter for inserting into database HOT 2
- Problemas de parseo con dos objetos y arraylist dentro de cada uno HELP HOT 2
- Support for generic elements
- Support for iso-8859-1 HOT 7
- No TypeAdapter for class HOT 2
- java: incompatible types: java.lang.Object cannot be converted to Book HOT 2
- Docs don't mention how to make a model class for an xml like the following HOT 3
- addTypeConverter
- Support for empty lists HOT 1
- "isXXX: String" compile error
- Plans to release 0.9.x?
- Update versions of okio and guava
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tikxml.