Skip to content

Commit

Permalink
Merge pull request #984 from pekkah/bug/microjson-dates-numbers-and-a…
Browse files Browse the repository at this point in the history
…rrays

microjson: some fixes for localization and arrays
  • Loading branch information
adrianstevens committed May 21, 2024
2 parents bb73c46 + 74b6be4 commit ba7290c
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static DateTime FromIso8601(string input)
/// <returns>DateTime as an ISO 8601 string in UTC format.</returns>
public static string ToIso8601(DateTime dateTime)
{
return $"{dateTime:yyyy-MM-ddTHH:mm:ss.FFFZ}";
return dateTime.ToString("o", CultureInfo.InvariantCulture);
}

/// <summary>
Expand All @@ -48,7 +48,7 @@ public static string ToIso8601(DateTime dateTime)
/// <returns>DateTime as an ISO 8601 string in UTC format.</returns>
public static string ToIso8601(DateTimeOffset dateTime)
{
return $"{dateTime:yyyy-MM-ddTHH:mm:ss.FFFZ}";
return dateTime.ToString("o", CultureInfo.InvariantCulture);
}

/// <summary>
Expand Down Expand Up @@ -84,4 +84,4 @@ public static DateTime FromASPNetAjax(string ajax)

return new DateTime(ticks, DateTimeKind.Utc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,20 @@ private static void Deserialize(Hashtable? root, Type type, ref object instance)
var targetArray = Array.CreateInstance(elementType, al!.Count);
for (int i = 0; i < al.Count; i++)
{
object arrayItem = Activator.CreateInstance(elementType);
Deserialize(al[i] as Hashtable, elementType, ref arrayItem);
targetArray.SetValue(arrayItem, i);
if (elementType == typeof(string))
{
targetArray.SetValue(al[i], i);
}
else if (elementType.IsValueType || elementType.IsEnum)
{
targetArray.SetValue(Convert.ChangeType(al[i], elementType), i);
}
else
{
object arrayItem = Activator.CreateInstance(elementType);
Deserialize(al[i] as Hashtable, elementType, ref arrayItem);
targetArray.SetValue(arrayItem, i);
}
}
prop.SetValue(instance, targetArray);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -99,7 +100,14 @@ public static string EscapeString(string value)
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
return o.ToString();
if (o is IFormattable formattable)
{
return formattable.ToString(null, System.Globalization.CultureInfo.InvariantCulture);
}
else
{
return o.ToString();
}
case TypeCode.DateTime:
return dateTimeFormat switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public void DeserializeDataWithContainsCarriageReturnsTest()
{
var input = """
{
"Value": "hello
"StringArg": "hello
there
data"
}
""";

var result = MicroJson.Deserialize<IntegerClass>(input);
var result = MicroJson.Deserialize<SimpleCommand>(input);

Assert.NotNull(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
<ItemGroup>
<None Include=".\Readme.md" Pack="true" PackagePath="" />
<None Remove="inputs\menu.json" />
<None Remove="inputs\open-meteo.json" />
<None Remove="inputs\puzzles.json" />
<None Remove="inputs\weather.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="inputs\menu.json" />
<EmbeddedResource Include="inputs\open-meteo.json" />
<EmbeddedResource Include="inputs\puzzles.json" />
<EmbeddedResource Include="inputs\weather.json" />
</ItemGroup>
Expand All @@ -37,8 +39,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj" />
<ProjectReference Include="..\..\Driver\Serialization.MicroJson.csproj" />
<ProjectReference Include="..\..\..\..\..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj" />
<ProjectReference Include="..\..\Driver\Serialization.MicroJson.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ public void CamelCasedWeatherDeserializationTest()
Assert.NotNull(result.Coord);
Assert.NotNull(result.Sys);
}

[Fact]
public void OpenMeteoJson()
{
var json = Inputs.GetInputResource("open-meteo.json");
var result = MicroJson.Deserialize<MeteoResponse>(json);

Assert.NotNull(result);
Assert.NotNull(result.current);
Assert.NotEmpty(result.hourly.temperature_2m);
Assert.NotEmpty(result.hourly.time);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"latitude": 60.17136,
"longitude": 24.927353,
"generationtime_ms": 0.11599063873291016,
"utc_offset_seconds": 10800,
"timezone": "Europe/Helsinki",
"timezone_abbreviation": "EEST",
"elevation": 12.0,
"current_units": {
"time": "iso8601",
"interval": "seconds",
"temperature_2m": "°C",
"relative_humidity_2m": "%"
},
"current": {
"time": "2024-05-20T13:45",
"interval": 900,
"temperature_2m": 19.0,
"relative_humidity_2m": 24
},
"hourly_units": {
"time": "iso8601",
"temperature_2m": "°C"
},
"hourly": {
"time": [ "2024-05-20T00:00", "2024-05-20T01:00", "2024-05-20T02:00", "2024-05-20T03:00", "2024-05-20T04:00", "2024-05-20T05:00", "2024-05-20T06:00", "2024-05-20T07:00", "2024-05-20T08:00", "2024-05-20T09:00", "2024-05-20T10:00", "2024-05-20T11:00", "2024-05-20T12:00", "2024-05-20T13:00", "2024-05-20T14:00", "2024-05-20T15:00", "2024-05-20T16:00", "2024-05-20T17:00", "2024-05-20T18:00", "2024-05-20T19:00", "2024-05-20T20:00", "2024-05-20T21:00", "2024-05-20T22:00", "2024-05-20T23:00", "2024-05-21T00:00", "2024-05-21T01:00", "2024-05-21T02:00", "2024-05-21T03:00", "2024-05-21T04:00", "2024-05-21T05:00", "2024-05-21T06:00", "2024-05-21T07:00", "2024-05-21T08:00", "2024-05-21T09:00", "2024-05-21T10:00", "2024-05-21T11:00", "2024-05-21T12:00", "2024-05-21T13:00", "2024-05-21T14:00", "2024-05-21T15:00", "2024-05-21T16:00", "2024-05-21T17:00", "2024-05-21T18:00", "2024-05-21T19:00", "2024-05-21T20:00", "2024-05-21T21:00", "2024-05-21T22:00", "2024-05-21T23:00", "2024-05-22T00:00", "2024-05-22T01:00", "2024-05-22T02:00", "2024-05-22T03:00", "2024-05-22T04:00", "2024-05-22T05:00", "2024-05-22T06:00", "2024-05-22T07:00", "2024-05-22T08:00", "2024-05-22T09:00", "2024-05-22T10:00", "2024-05-22T11:00", "2024-05-22T12:00", "2024-05-22T13:00", "2024-05-22T14:00", "2024-05-22T15:00", "2024-05-22T16:00", "2024-05-22T17:00", "2024-05-22T18:00", "2024-05-22T19:00", "2024-05-22T20:00", "2024-05-22T21:00", "2024-05-22T22:00", "2024-05-22T23:00", "2024-05-23T00:00", "2024-05-23T01:00", "2024-05-23T02:00", "2024-05-23T03:00", "2024-05-23T04:00", "2024-05-23T05:00", "2024-05-23T06:00", "2024-05-23T07:00", "2024-05-23T08:00", "2024-05-23T09:00", "2024-05-23T10:00", "2024-05-23T11:00", "2024-05-23T12:00", "2024-05-23T13:00", "2024-05-23T14:00", "2024-05-23T15:00", "2024-05-23T16:00", "2024-05-23T17:00", "2024-05-23T18:00", "2024-05-23T19:00", "2024-05-23T20:00", "2024-05-23T21:00", "2024-05-23T22:00", "2024-05-23T23:00", "2024-05-24T00:00", "2024-05-24T01:00", "2024-05-24T02:00", "2024-05-24T03:00", "2024-05-24T04:00", "2024-05-24T05:00", "2024-05-24T06:00", "2024-05-24T07:00", "2024-05-24T08:00", "2024-05-24T09:00", "2024-05-24T10:00", "2024-05-24T11:00", "2024-05-24T12:00", "2024-05-24T13:00", "2024-05-24T14:00", "2024-05-24T15:00", "2024-05-24T16:00", "2024-05-24T17:00", "2024-05-24T18:00", "2024-05-24T19:00", "2024-05-24T20:00", "2024-05-24T21:00", "2024-05-24T22:00", "2024-05-24T23:00", "2024-05-25T00:00", "2024-05-25T01:00", "2024-05-25T02:00", "2024-05-25T03:00", "2024-05-25T04:00", "2024-05-25T05:00", "2024-05-25T06:00", "2024-05-25T07:00", "2024-05-25T08:00", "2024-05-25T09:00", "2024-05-25T10:00", "2024-05-25T11:00", "2024-05-25T12:00", "2024-05-25T13:00", "2024-05-25T14:00", "2024-05-25T15:00", "2024-05-25T16:00", "2024-05-25T17:00", "2024-05-25T18:00", "2024-05-25T19:00", "2024-05-25T20:00", "2024-05-25T21:00", "2024-05-25T22:00", "2024-05-25T23:00", "2024-05-26T00:00", "2024-05-26T01:00", "2024-05-26T02:00", "2024-05-26T03:00", "2024-05-26T04:00", "2024-05-26T05:00", "2024-05-26T06:00", "2024-05-26T07:00", "2024-05-26T08:00", "2024-05-26T09:00", "2024-05-26T10:00", "2024-05-26T11:00", "2024-05-26T12:00", "2024-05-26T13:00", "2024-05-26T14:00", "2024-05-26T15:00", "2024-05-26T16:00", "2024-05-26T17:00", "2024-05-26T18:00", "2024-05-26T19:00", "2024-05-26T20:00", "2024-05-26T21:00", "2024-05-26T22:00", "2024-05-26T23:00" ],
"temperature_2m": [ 16.9, 16.3, 15.9, 15.0, 14.6, 14.6, 14.3, 14.9, 14.7, 14.8, 15.1, 15.7, 16.4, 18.1, 19.1, 19.7, 19.8, 19.5, 19.3, 19.1, 18.4, 17.6, 16.7, 16.2, 15.9, 15.3, 14.3, 13.0, 11.8, 10.8, 10.4, 10.8, 11.6, 13.0, 14.3, 14.9, 15.2, 15.8, 16.6, 17.2, 17.5, 17.6, 17.4, 17.2, 16.7, 15.9, 15.0, 14.3, 13.7, 13.3, 12.8, 12.0, 11.4, 11.1, 11.2, 11.6, 12.4, 13.1, 13.8, 14.6, 15.9, 17.2, 18.3, 18.6, 18.9, 19.1, 19.1, 18.8, 18.2, 17.7, 16.8, 12.6, 12.2, 12.0, 11.8, 11.5, 11.2, 11.0, 11.3, 12.1, 13.1, 14.0, 14.8, 15.5, 16.3, 16.6, 17.0, 17.2, 17.4, 17.6, 17.4, 16.8, 15.9, 15.1, 14.3, 13.6, 13.0, 12.8, 12.7, 12.7, 12.6, 12.5, 12.7, 13.3, 14.2, 15.1, 16.1, 17.2, 18.1, 18.7, 19.1, 19.4, 19.6, 19.6, 19.3, 18.5, 17.3, 16.3, 15.6, 15.1, 14.7, 14.5, 14.5, 14.4, 14.1, 13.8, 13.7, 14.2, 15.0, 15.6, 20.1, 21.4, 22.5, 23.3, 24.0, 24.3, 24.2, 23.8, 23.2, 22.4, 21.3, 20.2, 19.0, 17.7, 16.6, 16.0, 15.7, 15.3, 14.5, 13.7, 13.2, 13.5, 14.3, 15.0, 15.6, 16.2, 16.6, 16.6, 16.4, 16.2, 16.1, 15.9, 15.5, 14.9, 14.2, 13.4, 12.6, 11.8 ]
},
"daily_units": {
"time": "iso8601",
"weather_code": "wmo code",
"temperature_2m_max": "°C",
"temperature_2m_min": "°C"
},
"daily": {
"time": [ "2024-05-20", "2024-05-21", "2024-05-22", "2024-05-23", "2024-05-24", "2024-05-25", "2024-05-26" ],
"weather_code": [ 3, 3, 0, 0, 3, 80, 3 ],
"temperature_2m_max": [ 19.8, 17.6, 19.1, 17.6, 19.6, 24.3, 16.6 ],
"temperature_2m_min": [ 14.3, 10.4, 11.1, 11.0, 12.5, 13.7, 11.8 ]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Unit.Tests;

public class MeteoResponse
{
public float latitude { get; set; }
public float longitude { get; set; }
public float generationtime_ms { get; set; }
public int utc_offset_seconds { get; set; }
public string timezone { get; set; }
public string timezone_abbreviation { get; set; }
public float elevation { get; set; }
public Current_Units current_units { get; set; }
public Current current { get; set; }
public Hourly_Units hourly_units { get; set; }
public Hourly hourly { get; set; }
public Daily_Units daily_units { get; set; }
public Daily daily { get; set; }
}

public class Current_Units
{
public string time { get; set; }
public string interval { get; set; }
public string temperature_2m { get; set; }
public string relative_humidity_2m { get; set; }
}

public class Current
{
public string time { get; set; }
public int interval { get; set; }
public float temperature_2m { get; set; }
public int relative_humidity_2m { get; set; }
}

public class Hourly_Units
{
public string time { get; set; }
public string temperature_2m { get; set; }
}

public class Hourly
{
public string[] time { get; set; }
public float[] temperature_2m { get; set; }
}

public class Daily_Units
{
public string time { get; set; }
public string weather_code { get; set; }
public string temperature_2m_max { get; set; }
public string temperature_2m_min { get; set; }
}

public class Daily
{
public string[] time { get; set; }
public int[] weather_code { get; set; }
public float[] temperature_2m_max { get; set; }
public float[] temperature_2m_min { get; set; }
}

0 comments on commit ba7290c

Please sign in to comment.