[英]Json4s serialisation of superclass
I'm having trouble writing a custom serialiser for Json4s to handle the following situation. 我在为Json4s编写自定义序列化程序来处理以下情况时遇到麻烦。 I have case classes: 我有案例课:
trait Condition
case class BasicExpression (field:String, operator:String, value:String) extends Condition
case class BooleanExpression (val leftExpr: Condition, val logicalOperator:String,
val rightExpr: Condition) extends Condition
and I want to be able to read the JSON for both BasicExpression
and BooleanExpression
using, for example: 并且我希望能够使用以下示例读取BasicExpression
和BooleanExpression
的JSON:
var jsonStringBasic:String = """ {"field":"name","operator":"=","value":"adam"}""";
var jsonStringBoolean:String = """{"leftExpr":{"leftExpr":{"field":"field1", "operator":"=", "value":"value1"}, "logicalOperator":"AND", "rightExpr":{"field":"field2","operator":">","value":"500"}}, "logicalOperator":"AND", "rightExpr": {"field":"field3","operator":"<","value":"10000"}}""";
var jValueBasic:JValue = parse(jsonStringBasic, false);
var readCBasic = jValueBasic.extract[Condition];
I understand how the custom serialiser works for reading the BasicExpression
, and I could use SimpleTypeHints
, but it'd be good to not have to bloat the JSON for every Condition
. 我了解自定义序列化程序如何读取BasicExpression
,并且可以使用SimpleTypeHints
,但是最好不必为每个Condition
都BasicExpression
JSON。 I could also try extract[BooleanExpression]
and if that fails try extract[BasicExpression]
, but that seems ugly. 我也可以尝试extract[BooleanExpression]
,如果失败,请尝试extract[BasicExpression]
,但这看起来很丑。 Is it possible to write the custom serialiser to handle the fact that a BooleanCondition
will itself contain another Condition
recursively so I can extract[Condition]
? 是否可以编写自定义序列化程序来处理BooleanCondition
本身将递归包含另一个Condition
的事实,以便我可以extract[Condition]
?
for better parsing JSON you can try this : 为了更好地解析JSON,您可以尝试以下操作:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonUtils {
public static String defaultDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
private static GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat(defaultDateTimeFormat);
/***
* Creates a GSON instance from the builder with the default date/time format
*
* @return the GSON instance
*/
public static Gson createGson() {
// Create with default params
gsonBuilder = gsonBuilder.setDateFormat(defaultDateTimeFormat);
return gsonBuilder.create();
}
/***
* Creates a GSON instance from the builder specifying custom date/time format
*
* @return the GSON instance
*/
public static Gson createGson(String dateTimeFormat) {
// Create with the specified dateTimeFormat
gsonBuilder = gsonBuilder.setDateFormat(dateTimeFormat);
return gsonBuilder.create();
}
} }
and use it like this 像这样使用
var String = """ {"field":"name","operator":"=","value":"adam"}""";
Type collectionType = new TypeToken<YOUR_CLASS>() {}.getType();
YOUR_CLASS iii= GsonUtils.createGson().fromJson(jsonStringBasic, collectionType);
Managed to get the CustomSerializer working so it could call itself recursively, in the case of a BooleanExpression, as below: 设法使CustomSerializer工作,以便在BooleanExpression的情况下可以递归调用自己,如下所示:
class ConditionSerialiser extends CustomSerializer[Condition]( format => (
{
def deserialiseCondition:PartialFunction[JValue, Condition]= {
case JObject(List(JField("field", JString(field)), JField("operator", JString(operator)), JField("value", JString(value)))) => BasicStringExpression(field, operator, value)
case JObject(List(JField("field", JString(field)), JField("operator", JString(operator)), JField("value", JInt(value)))) => BasicNumericExpression(field, operator, value.doubleValue)
case JObject(List(JField("field", JString(field)), JField("operator", JString(operator)), JField("value", JDouble(value)))) => BasicNumericExpression(field, operator, value)
case JObject(List(JField("leftExpr", leftExpr), JField("logicalOperator", JString(logicalOperator)), JField("rightExpr", rightExpr))) => BooleanExpression(deserialiseCondition(leftExpr), logicalOperator, deserialiseCondition(rightExpr))
}
deserialiseCondition
},
{
case bse: BasicStringExpression => JObject(List(JField("field", JString(bse.field)), JField("operator", JString(bse.operator)), JField("value", JString(bse.value))))
}
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.