Skip to content

Commit

Permalink
fixed serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Nov 2, 2023
1 parent 2c0ff72 commit c1daa11
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 177 deletions.
358 changes: 192 additions & 166 deletions src/Stac.Api.Tests/CQL2Tests.cs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Stac.Api.Tests/Resources/CQL2/CQL2Tests_Example0.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
},
"LC08_L1TP_060247_20180905_20180912_01_T1_L1TP"
]
}
},
"filter-lang": "cql2-json"
}
3 changes: 2 additions & 1 deletion src/Stac.Api.Tests/Resources/CQL2/CQL2Tests_Example1.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
]
}
]
}
},
"filter-lang": "cql2-json"
}
48 changes: 48 additions & 0 deletions src/Stac.Api/Converters/AndOrExpressionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GeoJSON.Net.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stac.Api.Models;
using Stac.Api.Models.Cql2;

namespace Stac.Api.Converters
{
public class AndOrExpressionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(AndOrExpression);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
reader.DateParseHandling = DateParseHandling.None;
JObject jo = JObject.Load(reader);
return ReadJObject(jo, objectType, existingValue, serializer);
}

public AndOrExpression ReadJObject(JObject jo, Type objectType, object existingValue, JsonSerializer serializer)
{
return new AndOrExpression
{
Op = jo["op"].ToObject<AndOrExpressionOp>(serializer),
Args = jo["args"].ToObject<List<BooleanExpression>>()
};
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var expression = (AndOrExpression)value;
writer.WriteStartObject();
writer.WritePropertyName("op");
serializer.Serialize(writer, expression.Op);
writer.WritePropertyName("args");
serializer.Serialize(writer, expression.Args);
writer.WriteEndObject();
}
}


}
48 changes: 48 additions & 0 deletions src/Stac.Api/Converters/BinaryComparisonPredicateConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GeoJSON.Net.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stac.Api.Models;
using Stac.Api.Models.Cql2;

namespace Stac.Api.Converters
{
public class BinaryComparisonPredicateConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(AndOrExpression);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
reader.DateParseHandling = DateParseHandling.None;
JObject jo = JObject.Load(reader);
return ReadJObject(jo, objectType, existingValue, serializer);
}

public BinaryComparisonPredicate ReadJObject(JObject jo, Type objectType, object existingValue, JsonSerializer serializer)
{
return new BinaryComparisonPredicate
{
Op = jo["op"].ToObject<ComparisonPredicateOp>(serializer),
Args = jo["args"].ToObject<ScalarOperands>()
};
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var expression = (BinaryComparisonPredicate)value;
writer.WriteStartObject();
writer.WritePropertyName("op");
serializer.Serialize(writer, expression.Op);
writer.WritePropertyName("args");
serializer.Serialize(writer, expression.Args);
writer.WriteEndObject();
}
}


}
5 changes: 5 additions & 0 deletions src/Stac.Api/Converters/CQL2FilterConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public CQL2Expression ReadJObject(JObject jo, Type objectType, object existingVa

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
writer.WritePropertyName("filter-lang");
writer.WriteValue("cql2-json");
writer.WritePropertyName("filter");
serializer.Serialize(writer, (value as CQL2Expression).Expression);
writer.WriteEndObject();
}

private BooleanExpression CreateFilter(FilterLang? filter_lang, JObject filterParameter)
Expand Down
2 changes: 1 addition & 1 deletion src/Stac.Api/Converters/DateStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(((DateString)value).ToString());
writer.WriteValue(((DateString)value).ToString());
}
}
}
2 changes: 1 addition & 1 deletion src/Stac.Api/Converters/IIntervalItemConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
switch (value)
{
case IInstantString iis:
writer.WriteRawValue(iis.ToString());
writer.WriteValue(iis.DateTime);
break;
case PropertyRef pr:
serializer.Serialize(writer, pr);
Expand Down
8 changes: 7 additions & 1 deletion src/Stac.Api/Converters/IIsInListOperandConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
var operands = (ScalarExpressionCollection)value;
writer.WriteStartArray();
foreach (var operand in operands)
{
serializer.Serialize(writer, operand);
}
writer.WriteEndArray();
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/Stac.Api/Converters/ScalarOperandsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
JArray array = JArray.Load(reader);
return new ScalarOperands(array.ToObject<IScalarExpression[]>());
}
catch{}
catch { }

throw new JsonSerializationException($"Could not convert {reader.Value} to ScalarOperands");

}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
var operands = (ScalarOperands)value;
writer.WriteStartArray();
foreach (var operand in operands)
{
serializer.Serialize(writer, operand);
}
writer.WriteEndArray();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Stac.Api/Converters/StringExpressionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(((Models.Cql2.String)value).Str);
writer.WriteValue(((Models.Cql2.String)value).Str);
}
}
}
2 changes: 1 addition & 1 deletion src/Stac.Api/Converters/TimestampStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(((TimestampString)value).Timestamp.ToUniversalTime().ToString("O"));
writer.WriteValue(((TimestampString)value).Timestamp.ToUniversalTime().ToString("O"));
}
}
}
10 changes: 8 additions & 2 deletions src/Stac.Api/Models/Cql2/CQL2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Stac.Api.Models.Cql2
using GeoJSON.Net;
using GeoJSON.Net.Geometry;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Stac;
using Stac.Api.Converters;
using System = global::System;
Expand Down Expand Up @@ -84,7 +85,7 @@ public ScalarExpressionCollection AsScalarExpressionCollection()
}
}

[JsonConverter(typeof(NoConverter))]
[JsonConverter(typeof(AndOrExpressionConverter))]
public class AndOrExpression : BooleanExpression
{
[Newtonsoft.Json.JsonProperty("op", Required = Newtonsoft.Json.Required.Always)]
Expand Down Expand Up @@ -182,7 +183,7 @@ public abstract class ComparisonPredicate : BooleanExpression
public abstract IsNullPredicate AsIsNullPredicate();
}

[JsonConverter(typeof(NoConverter))]
[JsonConverter(typeof(BinaryComparisonPredicateConverter))]
public class BinaryComparisonPredicate : ComparisonPredicate
{
[Newtonsoft.Json.JsonProperty("op", Required = Newtonsoft.Json.Required.Always)]
Expand Down Expand Up @@ -407,6 +408,7 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti

public abstract DateTimeOffset DateTime { get; set; }

[JsonIgnore]
public IComparable Value => DateTime;

public BooleanExpression AsBooleanExpression()
Expand Down Expand Up @@ -1546,6 +1548,7 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti
set { _additionalProperties = value; }
}

[JsonIgnore]
public Itenso.TimePeriod.ITimeInterval TimeInterval
{
get
Expand Down Expand Up @@ -1633,6 +1636,7 @@ public StringIntervalItem(StringIntervalItemEnum v)
}
}

[JsonConverter(typeof(StringEnumConverter))]
public enum AndOrExpressionOp
{

Expand All @@ -1644,6 +1648,7 @@ public enum AndOrExpressionOp

}

[JsonConverter(typeof(StringEnumConverter))]
public enum NotExpressionOp
{

Expand All @@ -1652,6 +1657,7 @@ public enum NotExpressionOp

}

[JsonConverter(typeof(StringEnumConverter))]
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.16.1.0 (NJsonSchema v10.7.2.0 (Newtonsoft.Json v13.0.0.0))")]
public enum ComparisonPredicateOp
{
Expand Down

0 comments on commit c1daa11

Please sign in to comment.