Skip to content

Commit

Permalink
JsonSerializer improved for static array
Browse files Browse the repository at this point in the history
  • Loading branch information
Heromyth committed Dec 14, 2021
1 parent 9b33248 commit 32bab24
Showing 1 changed file with 80 additions and 12 deletions.
92 changes: 80 additions & 12 deletions source/hunt/serialization/JsonSerializer.d
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,11 @@ final class JsonSerializer {
}
}

/// Object array
/// Dynamic array
static T toObject(T : U[], SerializationOptions options = SerializationOptions.Default, U)
(auto ref const(JSONValue) json,
T defaultValue = T.init)
if (isArray!T && !isSomeString!T && !is(T : string) && !is(T
if (isDynamicArray!T && !isSomeString!T && !is(T : string) && !is(T
: wstring) && !is(T : dstring)) {

switch (json.type) {
Expand Down Expand Up @@ -428,6 +428,58 @@ final class JsonSerializer {
}
}

/// Static array
static T toObject(T : U[], SerializationOptions options = SerializationOptions.Default, U)
(auto ref const(JSONValue) json,
T defaultValue = T.init)
if (isStaticArray !T && !isSomeString!T && !is(T : string) && !is(T
: wstring) && !is(T : dstring)) {

enum ArrayLength = T.length;

switch (json.type) {
case JSONType.null_:
return T.init;

case JSONType.false_:
U[] r = [toObject!(U, options)(JSONValue(false))];
assert(r.length >= ArrayLength);
return r[0..ArrayLength];

case JSONType.true_:
U[] r = [toObject!(U, options)(JSONValue(true))];
assert(r.length >= ArrayLength);
return r[0..ArrayLength];

case JSONType.array:
U[] r = json.array
.map!(value => toObject!(U, options)(value))
.array
.to!(U[]);
assert(r.length >= ArrayLength);
return r[0..ArrayLength];

case JSONType.object:
return handleException!(T, options.canThrow())(json, "", defaultValue);

default:

return handleException!(T, options.canThrow())(json, "", defaultValue);
// try {
// U obj = toObject!(U, options.canThrow(true))(json);
// return [obj];
// } catch(Exception ex) {
// warning(ex.msg);
// version(HUNT_DEBUG) warning(ex);
// if(options.canThrow)
// throw ex;
// else {
// return [];
// }
// }
}
}

/// AssociativeArray
static T toObject(T : U[K], SerializationOptions options = SerializationOptions.Default, U, K)(
auto ref const(JSONValue) json, T defaultValue = T.init)
Expand Down Expand Up @@ -710,21 +762,30 @@ final class JsonSerializer {
} else static if(is(T == struct)) {
json = serializeObjectMember!(options)(m, serializationStates);
} else static if(is(T : U[], U)) {
if(m is null) {
static if(!options.ignoreNull) {
static if(isSomeString!T) {
json = toJson(m);
} else {
json = JSONValue[].init;
}
}
} else {
static if(isStaticArray!T) {
static if (is(U == class) || is(U == struct) || is(U == interface)) {
// class[] obj; struct[] obj;
json = serializeObjectMember!(options)(m, serializationStates);
} else {
json = toJson(m);
}
} else {
if(m is null) {
static if(!options.ignoreNull) {
static if(isSomeString!T) {
json = toJson(m);
} else {
json = JSONValue[].init;
}
}
} else {
static if (is(U == class) || is(U == struct) || is(U == interface)) {
// class[] obj; struct[] obj;
json = serializeObjectMember!(options)(m, serializationStates);
} else {
json = toJson(m);
}
}
}
} else {
json = toJson(m);
Expand Down Expand Up @@ -811,8 +872,15 @@ final class JsonSerializer {
return toJsonImpl!(options)(value, serializationStates);
}

private static JSONValue toJsonImpl(SerializationOptions options = SerializationOptions.Default, T: U[], U)(T data,
ref bool[size_t] serializationStates) if(isStaticArray!T) {

U[] value = data[0..$];
return toJsonImpl!(options)(value, serializationStates);
}

private static JSONValue toJsonImpl(SerializationOptions options = SerializationOptions.Default, T: U[], U)(T value,
ref bool[size_t] serializationStates) {
ref bool[size_t] serializationStates) if(isDynamicArray!T) {

static if(is(U == class)) { // class[]
if(value is null) {
Expand Down

0 comments on commit 32bab24

Please sign in to comment.