2015-11-18 45 views
0

我无法为Json4s编写自定义序列化程序来处理以下情况。我有case类:Json4s超类的序列化

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 

,我希望能够读取JSON两个BasicExpressionBooleanExpression使用,例如:

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]; 

我明白了定制串行器是如何工作的读取BasicExpression ,并且我可以使用SimpleTypeHints,但不必为每个Condition都膨胀JSON。我也可以尝试extract[BooleanExpression],如果失败尝试extract[BasicExpression],但看起来很难看。是否可以编写自定义序列化程序来处理这个事实:BooleanCondition本身将包含另一个Condition,因此我可以extract[Condition]

回答

0

设法让CustomSerializer工作,所以它可以递归调用本身,在布尔表达式的情况下,如下:

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))))  
} 
)) 
0

更好的解析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(); 
} 

}

,并使用它像这样

var String = """ {"field":"name","operator":"=","value":"adam"}"""; 

    Type collectionType = new TypeToken<YOUR_CLASS>() {}.getType(); 
    YOUR_CLASS iii= GsonUtils.createGson().fromJson(jsonStringBasic, collectionType); 
+0

感谢您的建议 - 我转向Gson了一下,但当Gson不支持Scala列表或地图时遇到了更多问题,于是我回到Json4s,想出了如何编写serialiser – adambriffett