最近很忙啊,新項目下來了,都沒時間寫博客了。頻率降低點,但不能不總結跟大家分享啊。
我們在項目里經常要涉及到各模塊間的通信,這其中又不可避免要碰到各類語言間之間的通信,比如之前做的一個項目里面就是Java發的消息需要C#接收,(具體消息是怎么傳輸的可以使用RabbitMQ等,關於RabbitMQ的使用總結可以看我之前的博客),都是面向對象的語言,而面向對象的消息怎么反解析到C#是個難題。下面就是使用Json密器讓Java和C#溝通的具體辦法的總結。
一、Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
public
class
NotifyRealTimeMessage
implements
Serializable {
private
static
ObjectMapper mapper =
new
ObjectMapper();
static
{
SimpleDateFormat dateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
mapper.setDateFormat(dateFormat);
}
@JsonProperty
(
"messageType"
)
private
int
type;
@JsonProperty
(
"geoData"
)
private
Object message;
@JsonProperty
(
"time"
)
private
Calendar time;
public
int
getType() {
return
type;
}
public
void
setType(
int
type) {
this
.type = type;
}
public
Object getMessage() {
return
message;
}
public
void
setMessage(Object message) {
this
.message = message;
}
public
Calendar getTime() {
return
time;
}
public
void
setTime(Calendar time) {
this
.time = time;
}
/**
* 產生Json串
*
*/
public
String toJson()
throws
JsonGenerationException,
JsonMappingException, IOException {
return
mapper.writeValueAsString(
this
);
}
/**
* 從Json字符串構建NotifyRealTimeMessage對象
*
*/
public
static
NotifyRealTimeMessage fromJson(String json)
throws
JsonParseException,
JsonMappingException, IOException {
if
(json ==
null
) {
return
null
;
}
else
{
return
mapper
.readValue(json, NotifyRealTimeMessage.
class
);
}
}
}
|
toJson方法將NotifyRealTimeMessage對象轉化為一個Json字符串,fromJson靜態方法將一個Json串轉化為一個NotifyRealTimeMessage對象,由於NotifyRealTimeMessage對象中包含一個時間類型的Calendar字段,故事先需要給mapper設定約定好的時間格式,mapper.SetDateFormat。這樣使用它:NotifyRealTimeMessage notifyMessage = NotifyRealTimeMessage.fromJson(json);String json=notifyMessage.toJson();。
二、C#
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
RealTimeDataMsg
{
[JsonProperty(
"messageType"
)]
public
int
MessageType {
get
;
set
; }
[JsonProperty(
"geoData"
)]
public
GeoData Data {
get
;
set
; }
[JsonProperty(
"time"
)]
public
DateTime Time {
get
;
set
; }
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public
static
class
JsonHelper
{
private
static
readonly
JsonSerializerSettings MyJsonSerializerSettings;
static
JsonHelper()
{
MyJsonSerializerSettings =
new
JsonSerializerSettings();
IsoDateTimeConverter dateTimeConverter =
new
IsoDateTimeConverter();
dateTimeConverter.DateTimeFormat =
"yyyy-MM-dd HH:mm:ss"
;
MyJsonSerializerSettings.Converters.Add(dateTimeConverter);
}
public
static
T FromJson<T>(
string
json)
{
if
(
string
.IsNullOrEmpty(json))
{
return
default
(T);
}
return
JsonConvert.DeserializeObject<T>(json, MyJsonSerializerSettings);
}
public
static
string
ToJson<T>(T data)
{
return
JsonConvert.SerializeObject(data, MyJsonSerializerSettings);
}
}
|
在C#中,使用起來也很方便,RealTimeDataMsg realMsg = JsonHelper.FromJson<RealTimeDataMsg>(json);string json = JsonHelper.ToJson(realMsg);。這里同樣需要給MyJsonSerializerSettings設置好事先約定的時間格式:yyyy-MM-dd HH:mm:ss,這樣才能正確的解析Java生成的Json串。
這樣,Java端和C#端都做好了,搞了一個新耳機,還沒煲好,煲耳機去嘍!