-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
166e54c
commit 57f3755
Showing
7 changed files
with
647 additions
and
6 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...in-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxBooleanTypeAdapter.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,47 @@ | ||
package me.chanjar.weixin.common.util.json; | ||
|
||
import com.google.gson.JsonParseException; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import org.apache.commons.lang3.BooleanUtils; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* <pre> | ||
* Gson 布尔类型类型转换器 | ||
* Created by Binary Wang on 2017-7-8. | ||
* </pre> | ||
* | ||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | ||
*/ | ||
public class WxBooleanTypeAdapter extends TypeAdapter<Boolean> { | ||
@Override | ||
public void write(JsonWriter out, Boolean value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
} else { | ||
out.value(value); | ||
} | ||
} | ||
|
||
@Override | ||
public Boolean read(JsonReader in) throws IOException { | ||
JsonToken peek = in.peek(); | ||
switch (peek) { | ||
case BOOLEAN: | ||
return in.nextBoolean(); | ||
case NULL: | ||
in.nextNull(); | ||
return null; | ||
case NUMBER: | ||
return BooleanUtils.toBoolean(in.nextInt()); | ||
case STRING: | ||
return BooleanUtils.toBoolean(in.nextString()); | ||
default: | ||
throw new JsonParseException("Expected BOOLEAN or NUMBER but was " + peek); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxDateTypeAdapter.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,43 @@ | ||
package me.chanjar.weixin.common.util.json; | ||
|
||
import com.google.gson.JsonParseException; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
/** | ||
* <pre> | ||
* Gson 日期类型转换器 | ||
* Created by Binary Wang on 2017-7-8. | ||
* </pre> | ||
* | ||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | ||
*/ | ||
public class WxDateTypeAdapter extends TypeAdapter<Date> { | ||
@Override | ||
public void write(JsonWriter out, Date value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
} else { | ||
out.value(value.getTime() / 1000); | ||
} | ||
} | ||
|
||
@Override | ||
public Date read(JsonReader in) throws IOException { | ||
JsonToken peek = in.peek(); | ||
switch (peek) { | ||
case NULL: | ||
in.nextNull(); | ||
return null; | ||
case NUMBER: | ||
return new Date(in.nextInt() * 1000); | ||
default: | ||
throw new JsonParseException("Expected NUMBER but was " + peek); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.