Skip to content

Commit e1038d8

Browse files
committed
Refactor to use consistent Utils.to/from .NET Custom Data Types
1 parent 3f1dbc2 commit e1038d8

File tree

15 files changed

+90
-154
lines changed

15 files changed

+90
-154
lines changed
0 Bytes
Binary file not shown.

src/AndroidClient/.idea/modules/android/AndroidClient.android.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AndroidClient/.idea/modules/client/AndroidClient.client.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AndroidClient/android/src/main/java/net/servicestack/client/ByteArray.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/AndroidClient/android/src/main/java/net/servicestack/client/DateTime.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/AndroidClient/android/src/main/java/net/servicestack/client/JsonSerializers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static JsonSerializer<Date> getDateSerializer(){
1818
return new JsonSerializer<Date>() {
1919
@Override
2020
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
21-
return src == null ? null : new JsonPrimitive(Utils.toJsonDate(src));
21+
return src == null ? null : new JsonPrimitive(Utils.toDateTime(src));
2222
}
2323
};
2424
}
@@ -27,7 +27,7 @@ public static JsonDeserializer<Date> getDateDeserializer(){
2727
return new JsonDeserializer<Date>() {
2828
@Override
2929
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
30-
return json == null ? null : Utils.parseDate(json.getAsString());
30+
return json == null ? null : Utils.fromDateTime(json.getAsString());
3131
}
3232
};
3333
}

src/AndroidClient/android/src/main/java/net/servicestack/client/Utils.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,6 @@ protected SimpleDateFormat initialValue()
214214
}
215215
};
216216

217-
public static String toJsonDate(Date date) {
218-
return "/Date(" + date.getTime() + "-0000)/";
219-
}
220-
221-
public static Date parseDate(String string) {
222-
String str = string.startsWith("\\")
223-
? string.substring(1)
224-
: string;
225-
226-
if (str.startsWith(wcfJsonPrefix)) {
227-
String body = splitOnLast(splitOnFirst(str, '(')[1], ')')[0];
228-
String unixTimeStr = splitOnFirst(body,'-', 1)[0];
229-
unixTimeStr = splitOnFirst(unixTimeStr,'+', 1)[0];
230-
long unixTime = Long.parseLong(unixTimeStr);
231-
return new Date(unixTime);
232-
}
233-
234-
return fromIsoDateString(string);
235-
}
236-
237217
public final static int isoDateLength = "YYYY-MM-DDT00:00:00+00:00".length();
238218
public final static int isoDateWithMsLength = "YYYY-MM-DDT00:00:00.000+00:00".length();
239219

@@ -715,40 +695,60 @@ public static <K,V> HashMap<K,ArrayList<V>> createMap(ArrayList<V> xs, Function<
715695
return to;
716696
}
717697

718-
public static String fromDateTime(Date date) {
719-
return DateTime.toString(date);
720-
}
698+
// From .NET DateTime (WCF JSON or ISO Date) to JVM Date
699+
public static Date fromDateTime(String jsonDate) {
700+
String str = jsonDate.startsWith("\\")
701+
? jsonDate.substring(1)
702+
: jsonDate;
703+
704+
if (str.startsWith(wcfJsonPrefix)) {
705+
String body = splitOnLast(splitOnFirst(str, '(')[1], ')')[0];
706+
String unixTimeStr = splitOnFirst(body,'-', 1)[0];
707+
unixTimeStr = splitOnFirst(unixTimeStr,'+', 1)[0];
708+
long unixTime = Long.parseLong(unixTimeStr);
709+
return new Date(unixTime);
710+
}
721711

722-
public static Date toDateTime(String jsonDate) {
723-
return DateTime.parse(jsonDate);
712+
return fromIsoDateString(jsonDate);
724713
}
725714

726-
public static String fromTimeSpan(TimeSpan timeSpan) {
727-
return TimeSpan.toString(timeSpan);
715+
// From JVM Date to .NET DateTime (WCF JSON Date)
716+
public static String toDateTime(Date date) {
717+
return "/Date(" + date.getTime() + "-0000)/";
728718
}
729719

730-
public static TimeSpan toTimeSpan(String xsdDuration) {
720+
// From .NET TimeSpan (XSD Duration) to Java TimeSpan
721+
public static TimeSpan fromTimeSpan(String xsdDuration) {
731722
return TimeSpan.parse(xsdDuration);
732723
}
733724

734-
public static String fromGuid(UUID uuid) {
735-
return Guid.toString(uuid);
725+
// From Java TimeSpan to .NET TimeSpan (XSD Duration)
726+
public static String toTimeSpan(TimeSpan timeSpan) {
727+
return TimeSpan.toString(timeSpan);
736728
}
737729

738-
public static UUID toGuid(String guid) {
730+
// From .NET Guid to JVM UUID
731+
public static UUID fromGuid(String guid) {
739732
return Guid.parse(guid);
740733
}
741734

742-
public static byte[] toByteArray(String base64) {
743-
return ByteArray.parse(base64);
735+
// From JVM UUID to .NET Guid
736+
public static String toGuid(UUID uuid) {
737+
return Guid.toString(uuid);
738+
}
739+
740+
// From .NET byte[] (Base64 String) to JVM signed byte[]
741+
public static byte[] fromByteArray(String base64) {
742+
return Base64.getDecoder().decode(base64);
744743
}
745744

746-
public static String toBase64String(String source) {
747-
return ByteArray.toString(source);
745+
// From JVM signed byte[] to .NET byte[] (Base64 String)
746+
public static String toByteArray(byte[] bytes) {
747+
return Base64.getEncoder().encodeToString(bytes);
748748
}
749749

750-
public static String fromByteArray(byte[] bytes) {
751-
return ByteArray.toString(bytes);
750+
public static String toBase64String(String source) {
751+
return toByteArray(Utils.toUtf8Bytes(source));
752752
}
753753

754754
public static String addQueryParam(String url, String key, String value) {

src/AndroidClient/client/src/main/java/net/servicestack/client/ByteArray.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/AndroidClient/client/src/main/java/net/servicestack/client/DateTime.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/AndroidClient/client/src/main/java/net/servicestack/client/JsonSerializers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static JsonSerializer<Date> getDateSerializer(){
1818
return new JsonSerializer<Date>() {
1919
@Override
2020
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
21-
return src == null ? null : new JsonPrimitive(Utils.toJsonDate(src));
21+
return src == null ? null : new JsonPrimitive(Utils.toDateTime(src));
2222
}
2323
};
2424
}
@@ -27,7 +27,7 @@ public static JsonDeserializer<Date> getDateDeserializer(){
2727
return new JsonDeserializer<Date>() {
2828
@Override
2929
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
30-
return json == null ? null : Utils.parseDate(json.getAsString());
30+
return json == null ? null : Utils.fromDateTime(json.getAsString());
3131
}
3232
};
3333
}

0 commit comments

Comments
 (0)