【问题标题】:Json4s serialisation of superclass超类的json4s序列化
【发布时间】:2015-11-18 11:55:26
【问题描述】:

我在为 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

我希望能够读取 BasicExpressionBooleanExpression 的 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];

我了解自定义序列化程序在读取BasicExpression 时的工作原理,我可以使用SimpleTypeHints,但最好不必为每个Condition 膨胀JSON。我也可以试试extract[BooleanExpression],如果失败,试试extract[BasicExpression],但这看起来很难看。是否可以编写自定义序列化程序来处理BooleanCondition 本身将递归包含另一个Condition 以便我可以extract[Condition] 的事实?

【问题讨论】:

    标签: java json scala json4s


    【解决方案1】:

    为了更好地解析 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);
    

    【讨论】:

    • 感谢您的推荐 - 我切换到了 Gson 一段时间,但当 Gson 不支持 Scala 列表或地图时遇到了进一步的问题,所以我回到 Json4s 并弄清楚如何编写序列化程序
    【解决方案2】:

    设法让 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))))       
    }
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多