Skip to content

Commit

Permalink
- Make java.util.Date/java.sql.Date is serialized as long (millisecon…
Browse files Browse the repository at this point in the history
…ds) in gson

- Update sentinel-demo-cluster-server-alone to demonstrate an example using fastjson in serialization
  • Loading branch information
jasonjoo2010 committed Sep 17, 2020
1 parent 8e6377d commit 6c0345c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# sentinel-demo-cluster-server-alone

Demo of standalone sentinel cluster mode.

## Special

It also demonstrate usage of `sentinel-serialization` with `fastjson` library only.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.serialization.common.fallback;

import java.lang.reflect.Type;
import java.util.Date;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

class DateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (typeOfT == java.sql.Date.class) {
return new java.sql.Date(json.getAsLong());
}
return new Date(json.getAsLong());
}

@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getTime());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static boolean check() {
actualVersionStr = String.valueOf(versionField.get(mainClass));
} catch (Exception e) {
}
INSTANCE = mainClass.newInstance();
INSTANCE = GsonFactory.create();
METHOD_TO_JSON_STRING = mainClass.getDeclaredMethod("toJson", Object.class);
METHOD_PARSE_OBJECT_BY_TYPE = mainClass.getDeclaredMethod("fromJson", String.class, Type.class);
METHOD_PARSE_OBJECT_BY_CLASS = mainClass.getDeclaredMethod("fromJson", String.class, Object.class.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.serialization.common.fallback;

import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* Used to create a shared Gson instance.
* <p>
* Compared to default one:
* <pre>
* - Make sure {@link java.util.Date} and {@link java.sql.Date}
* are serialized as milliseconds
* </pre>
*
* @author jason
*
*/
class GsonFactory {
public static Gson create() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
builder.registerTypeAdapter(java.sql.Date.class, new DateTypeAdapter());
return builder.create();
}
}

0 comments on commit 6c0345c

Please sign in to comment.