private object?DeserializeLeaf( object resultValue, ILeafType type, Path path, IInputField?field) { if (resultValue is IValueNode node) { return(ParseLeaf(node, type, path, field)); } try { return(type.Deserialize(resultValue)); } catch (SerializationException ex) { if (field is null) { throw new SerializationException(ex.Errors[0].WithPath(path), ex.Type, path); } IError error = ErrorBuilder.FromError(ex.Errors[0]) .SetPath(path) .SetExtension(nameof(field), field.Coordinate.ToString()) .SetExtension("fieldType", type.Name.Value) .Build(); throw new SerializationException(error, ex.Type, path); } }
public void IsInstanceOfType_Should_Pass_When_GeometryOfType(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act // assert Assert.True(type.IsInstanceOfType(_geometry)); }
public void IsInstanceOfType_Should_Fail_When_NoGeometry(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act // assert Assert.False(type.IsInstanceOfType("foo")); }
public void Serialize_Should_Pass_When_SerializeNullValue(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act // assert Assert.Null(type.Serialize(null)); }
public void ParseLiteral_Should_Pass_When_NullValueNode(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act // assert Assert.Null(type.ParseLiteral(NullValueNode.Default)); }
public void Serialize_Should_Throw_When_InvalidObjectShouldThrow(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act // assert Assert.Throws <SerializationException>(() => type.Serialize("")); }
private static double ToPrecision( ILeafType scalar, IValueNode valueSyntax, int precision = 8) { return(Math.Round( (double)scalar.ParseLiteral(valueSyntax) !, precision, MidpointRounding.AwayFromZero)); }
public void Serialize_Should_Pass_When_SerializeGeometry(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act var result = type.Serialize(_geometry); // assert result.MatchSnapshot(); }
public void Deserialize_Should_Pass_When_PassedSerializedResult(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); var serialized = type.Serialize(_geometry); // act var result = type.Deserialize(serialized); // assert Assert.True(Assert.IsAssignableFrom <Geometry>(result).Equals(_geometry)); }
public void IsInstanceOfType_Should_Fail_When_DifferentGeoJsonObject(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); // act // assert Assert.False( type.IsInstanceOfType( GeometryFactory.Default.CreateGeometryCollection( new Geometry[] { new Point(1, 2) }))); }
public void ParseValue_Should_Pass_When_Serialized(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); var serialized = type.Serialize(_geometry); // act IValueNode literal = type.ParseValue(serialized); // assert literal.ToString().MatchSnapshot(); }
public void Serialize_Should_Pass_When_Dictionary(string typeName) { // arrange ILeafType type = CreateLeafType(typeName); var dictionary = new Dictionary <string, object>(); // act var result = type.Serialize(dictionary); // assert Assert.Equal(dictionary, result); }
public LeafTypeModel( NameString name, string?description, ILeafType type, string serializationType, string runtimeType) { Name = name.EnsureNotEmpty(nameof(name)); Description = description; Type = type ?? throw new ArgumentNullException(nameof(type)); SerializationType = serializationType ?? throw new ArgumentNullException(nameof(serializationType)); RuntimeType = runtimeType ?? throw new ArgumentNullException(nameof(runtimeType)); }
private IValueNode FormatValueLeaf(object runtimeValue, ILeafType type, Path path) { try { if (runtimeValue.GetType() != type.RuntimeType && _converter.TryConvert(type.RuntimeType, runtimeValue, out var converted)) { runtimeValue = converted; } return(type.ParseValue(runtimeValue)); } catch (SerializationException ex) { throw new SerializationException(ex.Errors[0], ex.Type, path); } }
private IValueNode FormatResultLeaf(object resultValue, ILeafType type, Path path) { if (resultValue is IValueNode node) { if (type.IsInstanceOfType(node)) { return(node); } throw FormatResultLeaf_InvalidSyntaxKind(type, node.Kind, path); } try { return(type.ParseResult(resultValue)); } catch (SerializationException ex) { throw new SerializationException(ex.Errors[0], ex.Type, path); } }
private static void CompleteLeafType( ICompleteValueContext context, ILeafType leafType, object result) { try { if (TryConvertLeafValue( context.Converter, leafType, result, out object converted)) { context.Value = leafType.Serialize(converted); } else { context.AddError(b => b.SetMessage(string.Format( CultureInfo.InvariantCulture, CoreResources.CompleteLeafType_CannotConvertValue, leafType.TypeName()))); } } catch (ScalarSerializationException ex) { context.AddError(b => b.SetMessage(ex.Message) .SetException(ex)); } catch (Exception ex) { context.AddError(b => b.SetMessage(CoreResources .CompleteLeadType_UndefinedError) .SetException(ex)); } }
public NonLeafType(ILeafType dependency) { }
public static string GetRuntimeType(this ILeafType leafType) => (string)leafType.ContextData[RuntimeType] !;
public static string GetSerializationType(this ILeafType leafType) => (string)leafType.ContextData[SerializationType] !;
public LeafType(ILeafType type, IDictionary <string, object?> contextData) { Type = type; ContextData = contextData; }