Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse list with custom TypeConverter? #60

Open
landakram opened this issue May 11, 2015 · 13 comments
Open

Parse list with custom TypeConverter? #60

landakram opened this issue May 11, 2015 · 13 comments

Comments

@landakram
Copy link

Hello there!

I have a custom TypeConverter which extends StringBasedTypeConverter and converts a hex string to an Integer (it's a representation of a color).

The API I'm working with can return a single color (where this custom TypeConverter works great) or an array of colors. I'm super new to JSON parsing, and I'd like to use this custom TypeConverter to also deserialize an array of colors, but I can't quite figure out how to do this.

In summary, I can get "#0d9ddb" to the Integer type, and I'm trying to get ["#0d9ddb", "#00ba6e"] to the int[] or List<Integer> type. Does anyone know how to achieve this?

@EricKuck
Copy link
Member

I actually didn't even realize the library didn't support this until you pointed it out. Any suggestions on how you'd like to see this implemented? Changing the way the existing typeConverter parameter works in the case of collections seems like a bad idea, but I'm also hesitant to add another parameter just for this use case.

yongjhih added a commit to yongjhih/LoganSquare that referenced this issue Jun 16, 2015
@LouisCAD
Copy link

Hi!
I'm using Realm, Android Annotations, and your library LoganSquare.
Realm is a database (faster than SQLite) which store java objects extending their base class RealmObject. You can put RealmObjects inside RealmObjects, but for lists, you have to use RealmList (extending AbstractList).

Your library only allows ArraList and LinkedList, so I can't use both if my Json includes an array...
Could you call the default constructor of the class implementing list (RealmList for example), and then, fill it with the arraylist containing objects parsed from json using List#addAll(Collection) ?
If it's too long to do it, could you tell us how to build a List TypeConverter ?

Best regards

@berkayturanci
Copy link

Is there any progress on @LouisCO 's question? We also need that RealmList support too.

Thanks

@LouisCAD
Copy link

As I have a deadline for the whole app, I had to overcome this.

My workaround is to ignore the RealmList for LoganSquare, ignore the List for Realm and copy from RealmList to Json when it's time to send, and from list to realm when I have to save.

I do this in a custom HttpMessageConverter (from Spring Framework for Android), using awesome Android Annotations library.

import com.bluelinelabs.logansquare.annotation.JsonIgnore;
import io.realm.annotations.Ignore;
import static com.bluelinelabs.logansquare.annotation.JsonObject.FieldDetectionPolicy.NONPRIVATE_FIELDS_AND_ACCESSORS;

@JsonObject(fieldDetectionPolicy = NONPRIVATE_FIELDS_AND_ACCESSORS)
public class ObjectsWrapper extends RealmObject {

    @JsonIgnore private RealmList<MyObject> objects;
    @Ignore public List<MyObject> objectList;

    public RealmList<MyObject> getObjects() {
        return objects;
    }

    public void setActionPoints(RealmList<MyObject> objects) {
        this.objects= objects;
    }

Hope it's useful. Let me know if it's working for you.

yongjhih added a commit to yongjhih/LoganSquare that referenced this issue Jun 29, 2015
@berkayturanci
Copy link

@LouisCO Thanks for the answer, it is logical but seems hard to maintain. I mean, developer can forget to conversion btwn ArrayList <-> ReamList which is error prone.

Until a full support, we are gonna stick to the Gson.

Thanks

@aercanozcan
Copy link

Using this makes us hold a couple for each list and also makes an efficient crud operation impossible.

@LouisCAD
Copy link

If you have some time, you can fetch the library with git, edit it by adding a boolean flag useRealmLists, or by adding two new methods for serializing and parsing dedicated to RealmList. After testing, maybe the owner would accept you pull request

@krzysztof-miemiec
Copy link

krzysztof-miemiec commented Apr 27, 2016

I made this to make LoganSquare work with Realm lists:
https://gist.github.com/krzysztof-miemiec/d6b6449b0e0e56e4a57e4ed7bc367aab

@peterbetos
Copy link

I'm seeing that the issue seems to be addressed on https://github.com/krzysztof-miemiec/LoganSquare . Is it possible to explain how the typeConverter is used on the fork? Thanks.

@AndreasBackx
Copy link

@EricKuck any update on this?

@krzysztof-miemiec could you please share the how you addressed the issue?

@krzysztof-miemiec
Copy link

My github username got changed, so gist url stopped working. I fixed the link: https://gist.github.com/krzysztof-miemiec/d6b6449b0e0e56e4a57e4ed7bc367aab
The converter in gist allowed me to use @JsonObject-annotated RealmObjects in RealmLists to be serialized and deserialized easily.

@peterbetos Unfortunately I didn't address the list issue on my fork, but it allows to perform some operations after objects are created from json hierarchy and annotate getters and setters to be used instead of fields (this allows for more flexibility).

@AndreasBackx
Copy link

@krzysztof-miemiec thank you very much. It seems to be working out very well so far.

@selvan7
Copy link

selvan7 commented Mar 25, 2019

How to Implement Logansquare Custom TypeConverter class in the Inner Json object class? kindly give me a solution..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants