-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Make java.util.Date/java.sql.Date is serialized as long (millisecon…
…ds) in gson - Update sentinel-demo-cluster-server-alone to demonstrate an example using fastjson in serialization
- Loading branch information
1 parent
8e6377d
commit 6c0345c
Showing
5 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
sentinel-demo/sentinel-demo-cluster/sentinel-demo-cluster-server-alone/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...src/main/java/com/alibaba/csp/sentinel/serialization/common/fallback/DateTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...mon/src/main/java/com/alibaba/csp/sentinel/serialization/common/fallback/GsonFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |