Moshi自定义适配器参数类型及实现方式选型疑问求助
Hey there! Let's break down your two key questions about Moshi's adapter system—this stuff can be tricky at first, but once you grasp the differences, it all clicks.
1. Why do toJson/fromJson work with JsonWriter/JsonReader parameters?
First off, your ConfigAdapter working with those parameters is totally intentional—Moshi just doesn't highlight this in basic README examples.
Moshi's annotation-based adapters (using @ToJson/@FromJson) support multiple method signatures, not just the common "object ↔ string" ones you might have seen. Here's the breakdown:
- For
@FromJson: You can accept aJsonReaderparameter when you need full control over parsing the raw JSON stream. This is perfect for weird, non-standard JSON structures (like dynamic fields, deeply nested objects that need custom handling) where letting Moshi auto-parse might not cut it. Moshi will automatically pass the activeJsonReaderto your method when it needs to deserialize your type. - For
@ToJson: Accepting aJsonWriterplus your target object (likeWfBaseConfig?) lets you manually write every part of the JSON output. This is great if you want to enforce a specific field order, handle nulls in a custom way, or add extra logic that Moshi's default serialization doesn't support.
These signatures are recognized by Moshi's internal adapter factory—so as long as you have the right annotations, Moshi knows how to hook up your method to the serialization/deserialization pipeline.
2. When to inherit JsonAdapter<T> vs. using @ToJson/@FromJson?
The choice boils down to how much control you need and how complex your logic is:
Stick with @ToJson/@FromJson annotations if:
- You're only customizing behavior for a single class (or a small set of related classes).
- Your conversion logic is straightforward—like mapping an enum to a specific string value, or combining a couple of object fields into one JSON key.
- You want clean, concise code: Moshi automatically wraps your annotated methods into a
JsonAdapterinstance, so you don't have to handle low-level details like null safety or adapter lifecycle.
Inherit JsonAdapter<T> when:
- You need full, end-to-end control over the serialization/deserialization process. For example: handling dynamic types that change based on JSON content, adding custom error handling, or holding state in the adapter (like relying on another service to parse part of the data).
- You're building a generic adapter that works across multiple types (e.g., a custom adapter for all
Listsubclasses). - Annotation-based adapters can't solve your problem—like when you need to interact with the JSON stream in a way that annotations don't support (e.g., conditional field writing based on context).
As for that Unable to resolve Lcom/squareup/moshi/JsonAdapter; warning you saw: that's almost certainly a dependency issue. Double-check that:
- You've added the core Moshi dependency to your project (e.g.,
com.squareup.moshi:moshi:x.x.xfor Gradle). - If you're on Android, your proguard rules aren't stripping out Moshi's classes—you need to keep
com.squareup.moshi.**from being obfuscated. - Your Moshi version matches the API you're trying to use (old versions might have different class structures).
Since your annotation-based adapter works perfectly without inheriting JsonAdapter, there's no need to force that inheritance—stick with what's simpler for your use case!
内容的提问来源于stack exchange,提问作者Chenhe

