JRT引用的jackjson作為json處理庫。由于JRT.ORM要求表不用datetime類型,把日期和時間用Int存儲,所以O(shè)RM要支持日期時間的轉(zhuǎn)換。為什么要把日期時間不用datetime而用Int,比如日期:20240117,時間就是從0點到當(dāng)前的秒數(shù)。因為不用datetime兼容性好,不會因為不同庫datetime函數(shù)不同而要處理差異。用Int查詢效率高。
所以需要json序列化到實體時候能把日期和時間串序列化成整數(shù)。實體轉(zhuǎn)json時候要能把日期和時間的整數(shù)轉(zhuǎn)換成日期和時間串。
日期反序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
/**
* 把JSON串轉(zhuǎn)換對象,在日期屬性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTDateDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String val = node.asText();
return JRT.Core.Util.TimeParser.TransDate(val);
}
}
日期序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* 把對象轉(zhuǎn)換成JSON串,在日期屬性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTDateSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer dateInt, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(JRT.Core.Util.TimeParser.DateIntToStr(dateInt));
}
}
時間反序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
/**
* 把JSON串轉(zhuǎn)換對象,在時間屬性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTTimeDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String val = node.asText();
return JRT.Core.Util.TimeParser.TransTime(val);
}
}
時間序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* 把對象轉(zhuǎn)換成JSON串,在時間屬性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTTimeSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer timeStr, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(JRT.Core.Util.TimeParser.TimeIntToStr(timeStr));
}
}
布爾反序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
/**
* 把JSON串轉(zhuǎn)換對象,在布爾屬性上加上下面注解
* @JsonDeserialize(using = JRTBoolDeserializer.class)
*/
public class JRTBoolDeserializer extends JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
if(node.isBoolean())
{
Boolean val=node.asBoolean();
return val;
}
else if(node.isInt())
{
int val=node.asInt();
if(val==0)
{
return false;
}
else {
return true;
}
}
else
{
String val = node.asText();
if(val.equals("0"))
{
return false;
}
else if(val.equals("1"))
{
return true;
}
else if(val.equals("false"))
{
return false;
}
else if(val.equals("true"))
{
return true;
}
return false;
}
}
}
使用方式就是給實體屬性加上注解
由于ORM自己組json時候也需要知道哪些列是日期列,哪些是時間列,哪些是布爾列,所以自己實現(xiàn)了幾個注解,這里不用jackjson的注解來判斷日期時間就是為了可能變json庫做預(yù)留,自己可控注解更合適
日期注解
//特性,為通用碼表提供支持
package JRT.Core.CustomAttributes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 標(biāo)識屬性是日期的特性
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DateAttribute {
}
時間注解
//特性,為通用碼表提供支持
package JRT.Core.CustomAttributes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 標(biāo)識屬性是時間的特性
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeAttribute {
}
布爾注解
//特性,為通用碼表提供支持
package JRT.Core.CustomAttributes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 標(biāo)識屬性是布爾的特性
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BoolAttribute {
}
碼表按注解解析參數(shù)
ORM組json按注解處理日期和時間
對象轉(zhuǎn)json測試
打磨設(shè)計器
模板表實體文章來源:http://www.zghlxwxcb.cn/news/detail-801951.html
package JRT.Model.Entity;
import JRT.Core.CustomAttributes.*;
import JRT.Core.JsonAttributes.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
*[功能描述:本代碼由LIS內(nèi)部代碼生成工具生成,請不要手動修改,如要修改,請寫修改變更記錄]
*[創(chuàng)建者:JRT.Entity生成器]
*/
@UniqueAttribute(ColNames = "RowID")
@TableAttribute(Name="dbo.JRT_PrintTemplate")
public class JRTPrintTemplate
{
/**
* 主鍵
*/
@NotNullAttribute
@IdAttribute(Name = "RowID")
@LengthAttribute(MaxLen = 10)
public int RowID;
/**
* 模板代碼
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 100)
public String Code;
/**
* 模板名稱
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 100)
public String CName;
/**
* 產(chǎn)品組唯一標(biāo)識
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 30)
public String ProductGroup;
/**
* 產(chǎn)品組存的業(yè)務(wù)ID
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 30)
public String ProductBllID;
/**
* 模板紙張
*/
@FrekeyAttribute(Name = "JRTPrintPaper", RefColumnName = "RowID", AssociaField = "CName")
@LengthAttribute(MaxLen = 10)
public Integer JRTPrintPaperDR;
/**
* 邊距
*/
@LengthAttribute(MaxLen = 10)
public Double ResultLineSpacing;
/**
* 結(jié)果行數(shù)
*/
@LengthAttribute(MaxLen = 10)
public Integer ResultRows;
/**
* 結(jié)果列
*/
@LengthAttribute(MaxLen = 10)
public Integer ResultCols;
/**
* 紙張方向
*/
@LengthAttribute(MaxLen = 10)
public String PaperLayout;
/**
* 下邊距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperBottom;
/**
* 左邊距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperLeft;
/**
* 右邊距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperRight;
/**
* 上邊距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperTop;
/**
* 微生物邊距
*/
@LengthAttribute(MaxLen = 10)
public Double MicResultLineSpacing;
/**
* 微生物行數(shù)
*/
@LengthAttribute(MaxLen = 10)
public Integer MicResultRows;
/**
* 微生物列數(shù)
*/
@LengthAttribute(MaxLen = 10)
public Integer MicResultCols;
/**
* 邊距
*/
@LengthAttribute(MaxLen = 10)
public Double ArchivesMargin;
/**
* 雙列填充類型
*/
@LengthAttribute(MaxLen = 10)
public String DoubleColFillType;
/**
* 序號
*/
@LengthAttribute(MaxLen = 10)
public Integer Sequence;
/**
* 父模板
*/
@FrekeyAttribute(Name = "JRTPrintTemplate", RefColumnName = "RowID", AssociaField = "CName")
@LengthAttribute(MaxLen = 10)
public Integer ParentTemplateDR;
/**
* 截止日期
*/
@JsonSerialize(using = JRTDateSerializer.class)
@JsonDeserialize(using = JRTDateDeserializer.class)
@DateAttribute
@LengthAttribute(MaxLen = 10)
public Integer EndDate;
/**
* 截止時間
*/
@JsonSerialize(using = JRTTimeSerializer.class)
@JsonDeserialize(using = JRTTimeDeserializer.class)
@TimeAttribute
@LengthAttribute(MaxLen = 10)
public Integer EndTime;
/**
* 截止說明
*/
@LengthAttribute(MaxLen = 50)
public String EndRemark;
}
JRT的完善持續(xù)推進(jìn)中,抽取任何空余時間來打磨文章來源地址http://www.zghlxwxcb.cn/news/detail-801951.html
到了這里,關(guān)于jackjson自定義序列化和反序列化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!