Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 55 additions & 51 deletions dotnet/Zeiss.Micro.LibCzi.Net/Implementation/CziDocumentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace Zeiss.Micro.LibCzi.Net.Implementation
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using Zeiss.Micro.LibCzi.Net.Interface;
using Zeiss.Micro.LibCzi.Net.Interop;
Expand Down Expand Up @@ -62,25 +63,18 @@ public IReadOnlyDictionary<string, object> GetGeneralDocumentInfo()

// Parse the JSON string, and store the key-value pairs in the dictionary.
// We assume here that the root element must be an object.
using (JsonDocument document = JsonDocument.Parse(json))
{
JsonElement root = document.RootElement;
JObject root = JObject.Parse(json);

if (root.ValueKind == JsonValueKind.Object)
foreach (var property in root.Properties())
{
if (property.Name == CziDocumentPropertyKeys.GeneralDocumentInfoCreationDateTime)
{
foreach (JsonProperty property in root.EnumerateObject())
{
// Perform additional parsing for specific nodes if needed
if (property.Name == CziDocumentPropertyKeys.GeneralDocumentInfoCreationDateTime)
{
// Parse the "creation_data_time" node as a DateTime
dictionary[property.Name] = CziDocumentInfo.ParseCreationDateTime(property.Value);
}
else
{
dictionary[property.Name] = CziDocumentInfo.GetValue(property.Value);
}
}
// Parse the "creation_data_time" node as a DateTime
dictionary[property.Name] = CziDocumentInfo.ParseCreationDateTime(property.Value);
}
else
{
dictionary[property.Name] = CziDocumentInfo.GetValue(property.Value);
}
}

Expand All @@ -94,10 +88,10 @@ public DimensionIndex[] GetAvailableDimensions()
}

/// <inheritdoc/>
public JsonDocument GetDimensionInfo(DimensionIndex dimensionIndex)
public JObject GetDimensionInfo(DimensionIndex dimensionIndex)
{
string jsonText = LibCziApiInterop.Instance.CziDocumentInfoGetDimensionInfo(this.handle, dimensionIndex);
return JsonDocument.Parse(jsonText);
return JObject.Parse(jsonText);
}

/// <inheritdoc/>
Expand All @@ -107,53 +101,63 @@ public IDisplaySettings GetDisplaySettings()
return new DisplaySettings(displaySettingsHandle);
}

private static DateTime ParseCreationDateTime(JsonElement element)
private static DateTime ParseCreationDateTime(JToken token)
{
if (element.ValueKind == JsonValueKind.String)
if (token.Type == JTokenType.String)
{
string dateTimeString = element.GetString();
if (DateTime.TryParse(dateTimeString, null, System.Globalization.DateTimeStyles.RoundtripKind, out DateTime dateTime))
string dateTimeString = token.Value<string>();
if (DateTime.TryParseExact(dateTimeString, "O", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind, out DateTime dt))
{
return dt;
}

if (DateTime.TryParse(dateTimeString, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind, out dt))
{
return dt;
}

if (DateTime.TryParse(dateTimeString, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.RoundtripKind, out dt))
{
return dateTime;
return dt;
}

throw new FormatException($"Invalid date time format for 'creation_data_time': '{dateTimeString}'.");
}

if (token.Type == JTokenType.Date)
{
// Use the already-parsed DateTime value
return token.Value<DateTime>();
}

throw new FormatException("Invalid date time format for 'creation_data_time'.");
}

private static object GetValue(JsonElement element)
private static object GetValue(JToken token)
{
switch (element.ValueKind)
switch (token.Type)
{
case JsonValueKind.String:
return element.GetString();
case JsonValueKind.Number:
if (element.TryGetInt32(out int intValue))
case JTokenType.String:
return token.Value<string>();
case JTokenType.Integer:
// Try to fit into int, long, or just return as Int64
long longValue = token.Value<long>();
if (longValue >= int.MinValue && longValue <= int.MaxValue)
{
return intValue;
return (int)longValue;
}

if (element.TryGetInt64(out long longValue))
{
return longValue;
}

if (element.TryGetDouble(out double doubleValue))
{
return doubleValue;
}

break;
case JsonValueKind.True:
return true;
case JsonValueKind.False:
return false;
case JsonValueKind.Null:
return longValue;
case JTokenType.Float:
return token.Value<double>();
case JTokenType.Boolean:
return token.Value<bool>();
case JTokenType.Null:
return null;
default:
// Default case: return the token as a string
return token.ToString();
}

// Default case: return the element as a string
return element.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Zeiss.Micro.LibCzi.Net.Implementation
{
using System.Collections.Generic;
using System.Text.Json;
using Newtonsoft.Json;

/// <summary>
/// Here we collect some internal utilities that are used internally in the implementation of the library.
Expand All @@ -24,7 +24,7 @@ public static string FormatPropertyBagAsJson(IReadOnlyDictionary<string, object>
return string.Empty;
}

var jsonString = JsonSerializer.Serialize(parametersPropertyBag);
var jsonString = JsonConvert.SerializeObject(parametersPropertyBag);
return jsonString;
}
}
Expand Down
69 changes: 40 additions & 29 deletions dotnet/Zeiss.Micro.LibCzi.Net/Implementation/PyramidStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Zeiss.Micro.LibCzi.Net.Implementation
{
using System;
using System.Collections.Generic;
using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using Zeiss.Micro.LibCzi.Net.Interface;

Expand All @@ -29,43 +30,53 @@ public PyramidStatistics(string json)
this.pyramidLayerStatisticsPerScene = new Dictionary<int, IReadOnlyList<IPyramidLayerStatistics>>();

// Parse the JSON string
using (JsonDocument document = JsonDocument.Parse(json))
JObject root = JObject.Parse(json);

// Navigate to the "scenePyramidStatistics" object
JToken scenePyramidStatistics = root["scenePyramidStatistics"];
if (scenePyramidStatistics == null || scenePyramidStatistics.Type != JTokenType.Object)
{
// Navigate to the "scenePyramidStatistics" object
JsonElement root = document.RootElement;
JsonElement scenePyramidStatistics = root.GetProperty("scenePyramidStatistics");
throw new FormatException("Missing or invalid 'scenePyramidStatistics' object.");
}

// Iterate through the keys (e.g., "0", "1") and arrays
foreach (JsonProperty property in scenePyramidStatistics.EnumerateObject())
{
List<IPyramidLayerStatistics> pyramidLayerStatistics = new List<IPyramidLayerStatistics>();
// Iterate through the keys (e.g., "0", "1") and arrays
foreach (var property in ((JObject)scenePyramidStatistics).Properties())
{
List<IPyramidLayerStatistics> pyramidLayerStatistics = new List<IPyramidLayerStatistics>();

string key = property.Name;
if (string.IsNullOrWhiteSpace(key))
{
throw new FormatException("'key' cannot be null or whitespace.");
}
string key = property.Name;
if (string.IsNullOrWhiteSpace(key))
{
throw new FormatException("'key' cannot be null or whitespace.");
}

int sceneIndex = int.Parse(key);
int sceneIndex = int.Parse(key);

JsonElement array = property.Value;
JArray array = property.Value as JArray;
if (array == null)
{
throw new FormatException("Expected an array for scene '" + key + "'.");
}

// Iterate through each array element
foreach (JsonElement element in array.EnumerateArray())
// Iterate through each array element
foreach (JToken element in array)
{
JToken layerInfo = element["layerInfo"];
if (layerInfo == null)
{
JsonElement layerInfo = element.GetProperty("layerInfo");
byte minificationFactor = layerInfo.GetProperty("minificationFactor").GetByte();
byte pyramidLayerNo = layerInfo.GetProperty("pyramidLayerNo").GetByte();
int count = element.GetProperty("count").GetInt32();

PyramidLayerStatistics pyramidLayerStatistic = new PyramidLayerStatistics(
count,
new PyramidLayerInfo(minificationFactor, pyramidLayerNo));
pyramidLayerStatistics.Add(pyramidLayerStatistic);
throw new FormatException("Missing 'layerInfo' in pyramid layer statistics.");
}

this.pyramidLayerStatisticsPerScene.Add(sceneIndex, pyramidLayerStatistics);
byte minificationFactor = layerInfo["minificationFactor"].Value<byte>();
byte pyramidLayerNo = layerInfo["pyramidLayerNo"].Value<byte>();
int count = element["count"].Value<int>();

PyramidLayerStatistics pyramidLayerStatistic = new PyramidLayerStatistics(
count,
new PyramidLayerInfo(minificationFactor, pyramidLayerNo));
pyramidLayerStatistics.Add(pyramidLayerStatistic);
}

this.pyramidLayerStatisticsPerScene.Add(sceneIndex, pyramidLayerStatistics);
}
}

Expand Down
5 changes: 3 additions & 2 deletions dotnet/Zeiss.Micro.LibCzi.Net/Interface/ICziDocumentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Zeiss.Micro.LibCzi.Net.Interface
{
using System;
using System.Collections.Generic;
using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// This interface is used for retrieving parsed and consolidated metadata information from a CZI document.
Expand All @@ -31,7 +32,7 @@ public interface ICziDocumentInfo : IDisposable
/// <summary> Gets "dimension-info" for the specified dimension.</summary>
/// <param name="dimensionIndex"> The dimension to request "dimension info" for.</param>
/// <returns> The dimension information, formatted as JSON.</returns>
JsonDocument GetDimensionInfo(DimensionIndex dimensionIndex);
JObject GetDimensionInfo(DimensionIndex dimensionIndex);

/// <summary> Gets the display settings.</summary>
/// <returns> The display settings.</returns>
Expand Down
5 changes: 3 additions & 2 deletions dotnet/Zeiss.Micro.LibCzi.Net/Interop/LibCziApiInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Zeiss.Micro.LibCzi.Net.Interop
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using Newtonsoft.Json;

using Zeiss.Micro.LibCzi.Net.Implementation;
using Zeiss.Micro.LibCzi.Net.Interface;
Expand Down Expand Up @@ -2760,7 +2760,8 @@ private static Span<byte> AccessorAdditionalOptionsJsonFromPropertyBag(in Access
}

// Serialize directly to a UTF-8 byte array.
byte[] jsonBytes = JsonSerializer.SerializeToUtf8Bytes(jsonObject);
string jsonString = JsonConvert.SerializeObject(jsonObject);
byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString);

// Create a new array with space for the null terminator.
byte[] zeroTerminatedBytes = new byte[jsonBytes.Length + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="System.Memory" Version="4.6.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.1.1