Android 使用Moshi将自定义转换器转换为子类
我有一个用户类。和两个子类。父母和孩子。 我使用{“user”:“…”}从服务器获取json,需要根据user.type将其转换为父级或子级 据我所知,我需要通过以下方式添加自定义转换器:Android 使用Moshi将自定义转换器转换为子类,android,square,moshi,Android,Square,Moshi,我有一个用户类。和两个子类。父母和孩子。 我使用{“user”:“…”}从服务器获取json,需要根据user.type将其转换为父级或子级 据我所知,我需要通过以下方式添加自定义转换器: Moshi moshi = new Moshi.Builder() .add(new UserAdapter()) .build(); public class UserAdapter { @FromJson User fromJson(St
Moshi moshi = new Moshi.Builder()
.add(new UserAdapter())
.build();
public class UserAdapter {
@FromJson
User fromJson(String userJson) {
Moshi moshi = new Moshi.Builder().build();
try {
JSONObject jsonObject = new JSONObject(userJson);
String accountType = jsonObject.getString("type");
switch (accountType) {
case "Child":
JsonAdapter<Child> childJsonAdapter = moshi.adapter(Child.class);
return childJsonAdapter.fromJson(userJson);
case "Parent":
JsonAdapter<Parent> parentJsonAdapter = moshi.adapter(Parent.class);
return parentJsonAdapter.fromJson(userJson);
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
@ToJson
String toJson(User user) {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<User> jsonAdapter = moshi.adapter(User.class);
String toJson = jsonAdapter.toJson(user);
return toJson;
}
下面是我的UserAdapter实现。我知道这是假的,但即使这样也不行:
Moshi moshi = new Moshi.Builder()
.add(new UserAdapter())
.build();
public class UserAdapter {
@FromJson
User fromJson(String userJson) {
Moshi moshi = new Moshi.Builder().build();
try {
JSONObject jsonObject = new JSONObject(userJson);
String accountType = jsonObject.getString("type");
switch (accountType) {
case "Child":
JsonAdapter<Child> childJsonAdapter = moshi.adapter(Child.class);
return childJsonAdapter.fromJson(userJson);
case "Parent":
JsonAdapter<Parent> parentJsonAdapter = moshi.adapter(Parent.class);
return parentJsonAdapter.fromJson(userJson);
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
@ToJson
String toJson(User user) {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<User> jsonAdapter = moshi.adapter(User.class);
String toJson = jsonAdapter.toJson(user);
return toJson;
}
第二,我相信有更好的方法。请给我一些建议
Upd。下面是错误的stacktrace:
com.squareup.moshi.JsonDataException: Expected a name but was BEGIN_OBJECT at path $.user
at com.squareup.moshi.JsonReader.nextName(JsonReader.java:782)
at com.squareup.moshi.ClassJsonAdapter.fromJson(ClassJsonAdapter.java:141)
at com.squareup.moshi.JsonAdapter$1.fromJson(JsonAdapter.java:68)
at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:33)
at retrofit.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:33)
at retrofit.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:23)
at retrofit.OkHttpCall.parseResponse(OkHttpCall.java:148)
at retrofit.OkHttpCall.execute(OkHttpCall.java:116)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:111)
at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:88)
at rx.Observable$2.call(Observable.java:162)
at rx.Observable$2.call(Observable.java:154)
at rx.Observable$2.call(Observable.java:162)
at rx.Observable$2.call(Observable.java:154)
at rx.Observable.unsafeSubscribe(Observable.java:7710)
at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
在我看来,这似乎是您希望在自定义JSON数据反序列化/序列化时遵循的示例: 它使用一个对应于JSON结构的中间类,Moshi将为您自动膨胀它。然后,您可以使用膨胀的数据来构建专门的用户类。例如:
// Intermediate class with JSON structure
class UserJson {
// Common JSON fields
public String type;
public String name;
// Parent JSON fields
public String occupation;
public Long salary;
// Child JSON fields
public String favorite_toy;
public Integer grade;
}
abstract class User {
public String type;
public String name;
}
final class Parent extends User {
public String occupation;
public Long salary;
}
final class Child extends User {
public String favoriteToy;
public Integer grade;
}
现在,适配器:
class UserAdapter {
// Note that you pass in a `UserJson` object here
@FromJson User fromJson(UserJson userJson) {
switch (userJson.type) {
case "Parent":
final Parent parent = new Parent();
parent.type = userJson.type;
parent.name = userJson.name;
parent.occupation = userJson.occupation;
parent.salary = userJson.salary;
return parent;
case "Child":
final Child child = new Child();
child.type = userJson.type;
child.name = userJson.name;
child.favoriteToy = userJson.favorite_toy;
child.grade = userJson.grade;
return child;
default:
return null;
}
}
// Note that you return a `UserJson` object here.
@ToJson UserJson toJson(User user) {
final UserJson json = new UserJson();
if (user instanceof Parent) {
json.type = "Parent";
json.occupation = ((Parent) user).occupation;
json.salary = ((Parent) user).salary;
} else {
json.type = "Child";
json.favorite_toy = ((Child) user).favoriteToy;
json.grade = ((Child) user).grade;
}
json.name = user.name;
return json;
}
}
我认为这更干净,并且允许Moshi完成它的工作,即从JSON创建对象,从对象创建JSON。不要乱搞老式的JSONObject
要测试:
Child child = new Child();
child.type = "Child";
child.name = "Foo";
child.favoriteToy = "java";
child.grade = 2;
Moshi moshi = new Moshi.Builder().add(new UserAdapter()).build();
try {
// Serialize
JsonAdapter<User> adapter = moshi.adapter(User.class);
String json = adapter.toJson(child);
System.out.println(json);
// Output is: {"favorite_toy":"java","grade":2,"name":"Foo","type":"Child"}
// Deserialize
// Note the cast to `Child`, since this adapter returns `User` otherwise.
Child child2 = (Child) adapter.fromJson(json);
System.out.println(child2.name);
// Output is: Foo
} catch (IOException e) {
e.printStackTrace();
}
Child-Child=new-Child();
child.type=“child”;
child.name=“Foo”;
child.favoriteToy=“java”;
child.grade=2;
Moshi-Moshi=new Moshi.Builder().add(new UserAdapter()).build();
试一试{
//连载
JsonAdapter adapter=moshi.adapter(User.class);
String json=adapter.toJson(子项);
System.out.println(json);
//输出为:{“最喜欢的玩具”:“java”,“等级”:2,“名称”:“Foo”,“类型”:“Child”}
//反序列化
//注意强制转换为'Child',因为此适配器返回'User'。
Child child2=(Child)adapter.fromJson(json);
System.out.println(child2.name);
//输出为:Foo
}捕获(IOE异常){
e、 printStackTrace();
}
您可能试图根据以下内容实现解析:
这个字符串被用作@FromJson方法的参数,因此它可以神奇地解析为某个映射帮助器类或字符串,我们必须手动解析它,对吗?实际上不是,您可以使用映射帮助器类或映射
因此,您的异常需要一个字符串,但却是路径$处的BEGIN_对象。user
是由于Moshi试图将该用户作为字符串获取(因为这是您在适配器中暗示的),而它只是另一个对象
我不喜欢将所有可能的字段解析到某个helper类,因为在多态性的情况下,该类可能会变得非常大,您需要依赖或记住/注释代码
您可以将其作为映射处理(这是未知类型的默认模型),并将其转换为json,因此在您的情况下,它看起来类似于:
@FromJson
User fromJson(Map<String, String> map) {
Moshi moshi = new Moshi.Builder().build();
String userJson = moshi.adapter(Map.class).toJson(map);
try {
JSONObject jsonObject = new JSONObject(userJson);
String accountType = jsonObject.getString("type");
switch (accountType) {
case "Child":
JsonAdapter<Child> childJsonAdapter = moshi.adapter(Child.class);
return childJsonAdapter.fromJson(userJson);
case "Parent":
JsonAdapter<Parent> parentJsonAdapter = moshi.adapter(Parent.class);
return parentJsonAdapter.fromJson(userJson);
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
return null;
}
@FromJson
用户fromJson(映射){
Moshi-Moshi=新的Moshi.Builder().build();
字符串userJson=moshi.adapter(Map.class).toJson(Map);
试一试{
JSONObject JSONObject=新的JSONObject(userJson);
String accountType=jsonObject.getString(“类型”);
交换机(accountType){
“儿童”案:
JsonAdapter childJsonAdapter=moshi.adapter(Child.class);
返回childJsonAdapter.fromJson(userJson);
案例“家长”:
JsonAdapter parentJsonAdapter=moshi.adapter(Parent.class);
返回parentJsonAdapter.fromJson(userJson);
}
}捕获(JSONException | IOException e){
e、 printStackTrace();
}
返回null;
}
当然,您可以直接处理map:检索“type”字符串,然后将map的其余部分解析为所选类。这样就根本不需要使用JSONObject,因为它具有不依赖Android和更容易测试解析的优点
@FromJson
User fromJson(Map<String, String> map) {
Moshi moshi = new Moshi.Builder().build();
try {
String userJson = moshi.adapter(Map.class).toJson(map);
switch (map.get("type")) {
case "Child":
JsonAdapter<Child> childJsonAdapter = moshi.adapter(Child.class);
return childJsonAdapter.fromJson(userJson);
case "Parent":
JsonAdapter<Parent> parentJsonAdapter = moshi.adapter(Parent.class);
return parentJsonAdapter.fromJson(userJson);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@FromJson
用户fromJson(映射){
Moshi-Moshi=新的Moshi.Builder().build();
试一试{
字符串userJson=moshi.adapter(Map.class).toJson(Map);
开关(map.get(“type”)){
“儿童”案:
JsonAdapter childJsonAdapter=moshi.adapter(Child.class);
返回childJsonAdapter.fromJson(userJson);
案例“家长”:
JsonAdapter parentJsonAdapter=moshi.adapter(Parent.class);
返回parentJsonAdapter.fromJson(userJson);
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
现在有一种更好的方法可以做到这一点,使用PolymorphicJsonAdapterFactory
。请参见嘿,谢谢你的回复,我将在本周检查你的代码,如果正确,将其标记为corectworks@Defuera Ну как? 运气好吗?@savanto正在创建一个必要的适配器?这没有意义。你在哪里定义了你的User
类?我不认为这是“更干净”的。对我来说,有太多的地方会犯错误,而且随着更多的用户子类的出现,这个需要反序列化的普通类可能会变得非常大。