Moshi: Examples for JsonAdapter.Factory

Created on 22 Feb 2016  Â·  13Comments  Â·  Source: square/moshi

We don’t have anything in the readme, or any samples. We should probably add these.

documentation

Most helpful comment

@swankjesse for example, as a beginner, I don't get when should I create a Moshi instance with:
Moshi moshi = new Moshi.Builder().add(new MyCustomAdapter()).build();

Or
Moshi moshi = new Moshi.Builder().add(new MyCustomAdapterFactory()).build();
That is, an Adapter or an Adapter Factory.

(I also don't understand when should I create an Adapter that extends JsonAdapter or one there uses annotations such as @ToJson, which are btw the only type the documentations mention)

All 13 comments

@swankjesse it would be nice to have some documentation explaining the purpose of a JsonAdapter.Factory. Maybe this is obvious for the more experienced, but it's hard for the beginners to understand when it should be used and all it can do.

@swankjesse for example, as a beginner, I don't get when should I create a Moshi instance with:
Moshi moshi = new Moshi.Builder().add(new MyCustomAdapter()).build();

Or
Moshi moshi = new Moshi.Builder().add(new MyCustomAdapterFactory()).build();
That is, an Adapter or an Adapter Factory.

(I also don't understand when should I create an Adapter that extends JsonAdapter or one there uses annotations such as @ToJson, which are btw the only type the documentations mention)

Not in a position to provide a longer answer right now, but to your first
point, an adapter is for a specific type, a factory is called for every
type and gets to choose whether to return an adapter or ignore it (return
null).

On Mon, Jan 8, 2018 at 10:29 AM feinstein notifications@github.com wrote:

@swankjesse https://github.com/swankjesse for example, as a beginner, I
don't get when should I create a Moshi instance with:

Moshi moshi = new Moshi.Builder().add(new MyCustomAdapter()).build();

Or

Moshi moshi = new Moshi.Builder().add(new MyCustomAdapterFactory()).build();

(I also don't understand when should I create an Adapter that extends
JsonAdapter or one there uses annotations such as @ToJson, which are btw
the only type the documentations mention)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/136#issuecomment-355998142, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEbVsyLdMzHk2KyFFUxKEx7p2YjeTks5tIjRQgaJpZM4Hf6ug
.

I see, so passing an Adapter should be a lot better right? I guess the Adapter will be reused, while the factory will be creating a new adapter all the time, besides the performance hit for scanning multiple factories.

If this is true, Moshi should have a way to get a JSON array adapter without the need for a factory being passed to the Builder, since this is a very common use case.

No, it doesn't matter. The factory is only called once per-type. If it
returns an adapter that instance is cached.

On Mon, Jan 8, 2018 at 11:55 AM feinstein notifications@github.com wrote:

I see, so passing an Adapter should be a lot better right? I guess the
Adapter will be reused, while the factory will be creating a new adapter
all the time, besides the performance hit for scanning multiple factories.

If this is true, Moshi should have a way to get a JSON array adapter
without the need for a factory being passed to the Builder, since this is a
very common use case.

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/136#issuecomment-356025425, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEERKwRoIzQXS0FzBhCX2TcSedLDjAks5tIkiOgaJpZM4Hf6ug
.

Oh that's very nice.....but still feels a bit cumbersome to make an entire Factory (instead to just 1 line of code) to parse a JSON array to List, Collection, Iterable and etc.

That code only has to be written once so it's acceptable that it requires a
few lines.

On Mon, Jan 8, 2018 at 12:00 PM feinstein notifications@github.com wrote:

Oh that's very nice.....but still feels a bit cumbersome to make an entire
Factory (instead to just 1 line of code) to parse a JSON array to List,
Collection, Iterable and etc.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/136#issuecomment-356026874, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEUjCwpKPCl9HZUWqY_YkAuqwFM27ks5tIkmcgaJpZM4Hf6ug
.

I agree, but maybe it should come packed with Moshi, so we would just add the JsonArrayAdapterFactory<T> to the Builder?

I understand Moshi is very streamlined, but AFAICS this is such a regular use case, people will be doing it anyway.

Just an idea, I appreciate you guys being welcome to user input :)

I'm not sure this is a common case.

You should also be able to write a @FromJson method with a
JsonAdapter> argument to get access to a delegate adapter.

On Mon, Jan 8, 2018 at 12:12 PM feinstein notifications@github.com wrote:

Just an idea, I appreciate you guys being welcome to user input :)

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/136#issuecomment-356030602, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEV2P0I8DAltwTwfuvBv5hQ6mnyjzks5tIkySgaJpZM4Hf6ug
.

Am I sorry @JakeWharton, now I am getting very confused.

When you said you don't think that JSONs with arrays of data isn't a common case, I decided to read the documentations again to see if we are talking about the same stuff and I see the documentations say indeed that Arrays, Collections, Lists, Sets, and Maps are Built-in Type Adapters, which makes perfect sense to me as I think this is a very common case.

But later the documentation shows how to Parse JSON Arrays and shows that we are supposed to create a List<Card> Adapter. This led me to conclude that I should make my own List Adapter ALWAYS, but indeed I don't need to, I removed it altogether from my project and it works just fine.

So this leads me to some questions:

1 - Why does the documents show how to create a List Adapter, if they are already built into Moshi?

2 - What were you referring to when you said that isn't a common case?

That code is intended to show you how to get Moshi’s List<Card> adapter, not how to create it.

I see... Thank you for being patient!

Was this page helpful?
0 / 5 - 0 ratings