Toggle navigation
Toggle navigation
This project
Loading...
Sign in
yangyoupeng
/
zhaoonline-utils
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
yangyoupeng
2016-11-01 19:34:51 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
63868c771244feb618d3a6d0b22e664e5e359412
63868c77
1 parent
0e7a2cc8
添加Date类型的Json序列化和反序列化的器
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
115 additions
and
1 deletions
src/main/java/com/zhaoonline/common/json/JsonDateDeserializer.java
src/main/java/com/zhaoonline/common/json/JsonUtils.java
src/test/java/com/zhaoonline/common/es/TestESClient.java
src/test/java/com/zhaoonline/common/json/TestDateObject.java
src/test/java/com/zhaoonline/common/json/TestJsonDateSerializer.java
src/main/java/com/zhaoonline/common/json/JsonDateDeserializer.java
0 → 100644
View file @
63868c7
package
com
.
zhaoonline
.
common
.
json
;
import
java.io.IOException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
com.fasterxml.jackson.core.JsonParser
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.DeserializationContext
;
import
com.fasterxml.jackson.databind.JsonDeserializer
;
public
class
JsonDateDeserializer
extends
JsonDeserializer
<
Date
>{
private
static
final
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
@Override
public
Date
deserialize
(
JsonParser
jsonparser
,
DeserializationContext
ctxt
)
throws
IOException
,
JsonProcessingException
{
String
date
=
jsonparser
.
getText
();
try
{
return
dateFormat
.
parse
(
date
);
}
catch
(
ParseException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
src/main/java/com/zhaoonline/common/json/JsonUtils.java
View file @
63868c7
...
...
@@ -2,17 +2,29 @@ package com.zhaoonline.common.json;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Date
;
import
java.util.Map
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JsonDeserializer
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.module.SimpleModule
;
public
class
JsonUtils
{
private
static
ObjectMapper
mapper
=
new
ObjectMapper
();
static
{
SimpleModule
module
=
new
SimpleModule
();
module
.
addSerializer
(
Date
.
class
,
new
JsonDateSerializer
());
mapper
.
registerModule
(
module
);
JsonDateDeserializer
deser
=
new
JsonDateDeserializer
();
module
.
addDeserializer
(
Date
.
class
,
deser
);
mapper
.
registerModule
(
module
);
}
/**
* Method name: toObject <BR>
* Description: 读取失败就返回null <BR>
...
...
src/test/java/com/zhaoonline/common/es/TestESClient.java
View file @
63868c7
...
...
@@ -2,6 +2,7 @@ package com.zhaoonline.common.es;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
...
...
@@ -19,6 +20,7 @@ import com.zhaoonline.common.es.bean.QueryResponse;
import
com.zhaoonline.common.es.bean.TermQuery
;
import
com.zhaoonline.common.es.bean.TermsQuery
;
import
com.zhaoonline.common.json.JsonUtils
;
import
com.zhaoonline.common.json.TestDateObject
;
public
class
TestESClient
{
...
...
@@ -256,4 +258,25 @@ public class TestESClient {
Assert
.
assertTrue
(
result
);
}
@Test
public
void
testBulkAddDate
()
throws
UnsupportedOperationException
,
IOException
{
String
index
=
"zhaoon1"
;
String
type
=
"test1"
;
ESClientConfiguration
config
=
new
ESClientConfiguration
();
config
.
addHostPorts
(
"192.168.0.162:9200"
);
ESHttpClient
client
=
new
ESHttpClient
(
index
,
type
,
config
);
client
.
init
();
List
ids
=
new
ArrayList
<>();
List
dataList
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
100
;
i
++){
TestDateObject
o
=
new
TestDateObject
();
o
.
setId
(
i
);
o
.
setDate
(
new
Date
(
System
.
currentTimeMillis
()));
dataList
.
add
(
o
);
ids
.
add
(
i
);
}
boolean
result
=
client
.
bulkAddDoc
(
ids
,
dataList
);
Assert
.
assertTrue
(
result
);
}
}
...
...
src/test/java/com/zhaoonline/common/json/TestDateObject.java
View file @
63868c7
...
...
@@ -5,7 +5,6 @@ import java.util.Date;
import
com.fasterxml.jackson.databind.annotation.JsonSerialize
;
public
class
TestDateObject
{
@JsonSerialize
(
using
=
JsonDateSerializer
.
class
)
public
Date
date
;
public
Date
getDate
()
{
...
...
@@ -16,4 +15,26 @@ public class TestDateObject {
this
.
date
=
date
;
}
public
String
name
;
public
Integer
id
;
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
}
...
...
src/test/java/com/zhaoonline/common/json/TestJsonDateSerializer.java
View file @
63868c7
package
com
.
zhaoonline
.
common
.
json
;
import
java.io.IOException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.module.SimpleModule
;
public
class
TestJsonDateSerializer
{
@Test
public
void
testDateParse
()
throws
JsonProcessingException
{
ObjectMapper
mapper
=
new
ObjectMapper
();
SimpleModule
module
=
new
SimpleModule
();
module
.
addSerializer
(
Date
.
class
,
new
JsonDateSerializer
());
mapper
.
registerModule
(
module
);
TestDateObject
o
=
new
TestDateObject
();
o
.
setDate
(
new
Date
(
System
.
currentTimeMillis
()));
System
.
out
.
println
(
mapper
.
writeValueAsString
(
o
));;
}
@Test
public
void
testDateParseWithJsonUtils
()
throws
JsonProcessingException
{
TestDateObject
object
=
new
TestDateObject
();
object
.
setDate
(
new
Date
(
System
.
currentTimeMillis
()));
System
.
out
.
println
(
JsonUtils
.
toJson
(
object
));;
}
@Test
public
void
testDateDeserialWithJsonUtils
()
throws
ParseException
,
IOException
{
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
String
testDateString
=
"{\"date\":\"2016-11-01 19:28:38\",\"name\":null,\"id\":null}"
;
TestDateObject
testDate
=
JsonUtils
.
toObject
(
testDateString
,
TestDateObject
.
class
);
Assert
.
assertEquals
(
dateFormat
.
parse
(
"2016-11-01 19:28:38"
)
,
testDate
.
getDate
());
Assert
.
assertNull
(
testDate
.
getId
());
Assert
.
assertNull
(
testDate
.
getName
());
}
}
...
...
Please
register
or
login
to post a comment