Compare commits

...

3 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 47b2a318da Package
- Documented public import

Exceptions

- Documented module and classes
2023-06-28 11:56:01 +02:00
Tristan B. Velloza Kildaire a15e0b5bcb Serializer
- Cleaned up
2023-06-28 11:55:09 +02:00
Tristan B. Velloza Kildaire dad7e04ccf Exceptions
- Added `DeserializationError`

Deserializer

- On error now throws `DeserializationError` exception
2023-06-28 11:51:57 +02:00
4 changed files with 26 additions and 8 deletions

View File

@ -4,7 +4,7 @@
module jstruct.deserializer; module jstruct.deserializer;
import std.json; import std.json;
import jstruct.exceptions : SerializationError; import jstruct.exceptions : DeserializationError;
import std.traits : FieldTypeTuple, FieldNameTuple, isArray; import std.traits : FieldTypeTuple, FieldNameTuple, isArray;
/** /**
@ -191,8 +191,7 @@ public RecordType fromJSON(RecordType)(JSONValue jsonIn)
} }
catch(JSONException e) catch(JSONException e)
{ {
// TOOD: Should be DEserialization error throw new DeserializationError();
throw new SerializationError();
} }
} }
@ -287,7 +286,7 @@ unittest
Person person = fromJSON!(Person)(json); Person person = fromJSON!(Person)(json);
assert(false); assert(false);
} }
catch(SerializationError) catch(DeserializationError)
{ {
assert(true); assert(true);
} }

View File

@ -1,5 +1,11 @@
/**
* Exception types
*/
module jstruct.exceptions; module jstruct.exceptions;
/**
* General exception type
*/
public abstract class JStructException : Exception public abstract class JStructException : Exception
{ {
this(string msg) this(string msg)
@ -8,6 +14,9 @@ public abstract class JStructException : Exception
} }
} }
/**
* Error on serialization
*/
public final class SerializationError : JStructException public final class SerializationError : JStructException
{ {
this() this()
@ -15,3 +24,14 @@ public final class SerializationError : JStructException
super("Error serializing"); super("Error serializing");
} }
} }
/**
* Error on deserialization
*/
public final class DeserializationError : JStructException
{
this()
{
super("Error deserializing");
}
}

View File

@ -13,4 +13,7 @@ public import jstruct.serializer;
*/ */
public import jstruct.deserializer; public import jstruct.deserializer;
/**
* Exception types
*/
public import jstruct.exceptions; public import jstruct.exceptions;

View File

@ -34,9 +34,6 @@ public JSONValue serializeRecord(RecordType)(RecordType record)
// pragma(msg, structValues[cnt]); // pragma(msg, structValues[cnt]);
} }
import std.traits : isArray;
static if(__traits(isSame, structTypes[cnt], int)) static if(__traits(isSame, structTypes[cnt], int))
{ {
builtJSON[structNames[cnt]] = structValues[cnt]; builtJSON[structNames[cnt]] = structValues[cnt];
@ -68,7 +65,6 @@ public JSONValue serializeRecord(RecordType)(RecordType record)
else static if(isArray!(structTypes[cnt])) else static if(isArray!(structTypes[cnt]))
{ {
builtJSON[structNames[cnt]] = structValues[cnt]; builtJSON[structNames[cnt]] = structValues[cnt];
// pragma(msg, "WAIT", "d"~mixin(structTypes[cnt]));
} }
else else
{ {