Git Product home page Git Product logo

Comments (28)

sockeqwe avatar sockeqwe commented on September 26, 2024 1

its not implemented properly for lists. this will be fixed with a future snapshot release

from tikxml.

sockeqwe avatar sockeqwe commented on September 26, 2024 1

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.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

This is also needed for the support of a Map for example.

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

With moshi for example you can use something like this Type realmIntegerList = Types.newParameterizedType(RealmInteger.class, RealmList.class);

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

And for maps moshi use Type[] keyAndValue = Types.mapKeyAndValueTypes(type, rawType);

from tikxml.

sockeqwe avatar sockeqwe commented on September 26, 2024

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.

meierjan avatar meierjan commented on September 26, 2024

Hi, currently trying to use TikXml for RealmList's. Any known workarounds?

from tikxml.

sockeqwe avatar sockeqwe commented on September 26, 2024

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.

meierjan avatar meierjan commented on September 26, 2024

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.

sockeqwe avatar sockeqwe commented on September 26, 2024

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.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

Great feature. πŸ‘ . Awesome work.

from tikxml.

meierjan avatar meierjan commented on September 26, 2024

Wow, that's the low priority speed? Really awesome work!

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

One question, how can i prevent the annotation processor to generate a wrong ChildElementBinder that is using new ArrayList?

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

So that the annotation processor is using the getTypeAdapter(type) and not getTypeAdapter(class)

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

if (value.getObjects() == null) {
value.setObjects(new ArrayList());
}

is the problem

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

.add(accessPolicy.resolveAssignment("new \$T()", valueTypeAsArrayList))
there is the problem.

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

apt {
arguments {
" "//this canΒ΄t accept stuff like io.realm.RealmList
}
}

from tikxml.

sockeqwe avatar sockeqwe commented on September 26, 2024

Indeed, list are a special edge case. I'm rewriting the code generator right now and will consider that ...

from tikxml.

FabianTerhorst avatar FabianTerhorst commented on September 26, 2024

ok thanks.

from tikxml.

meierjan avatar meierjan commented on September 26, 2024

@FabianTerhorst could you manage to parse them? I failed.... (even in 0.8.1-SNAPSHOT)

from tikxml.

meierjan avatar meierjan commented on September 26, 2024

Any ETA? (rly just looking forward to test this)

from tikxml.

sockeqwe avatar sockeqwe commented on September 26, 2024

from tikxml.

meierjan avatar meierjan commented on September 26, 2024

Yeah, please. A short introduction would be great.

from tikxml.

AleSpero avatar AleSpero commented on September 26, 2024

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.

AleSpero avatar AleSpero commented on September 26, 2024

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.

sockeqwe avatar sockeqwe commented on September 26, 2024
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.

komuros avatar komuros commented on September 26, 2024

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.

CppGeekMajor avatar CppGeekMajor commented on September 26, 2024

Hello, how and where is RealmList declared?

from tikxml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.