using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonEvalRs
{
public partial class JSONEval
{
public void EvaluateSubform(string subformPath, string data, string? context = null, IEnumerable<string>? paths = null)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
string? pathsJson = paths != null ? JsonConvert.SerializeObject(paths) : null;
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_evaluate_subform(_handle, subformPath, data, context, pathsJson);
#else
var result = Native.json_eval_evaluate_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(data)!, Native.ToUTF8Bytes(context), Native.ToUTF8Bytes(pathsJson));
#endif
if (!result.Success)
{
#if NETCOREAPP || NET5_0_OR_GREATER
string error = result.Error != IntPtr.Zero
? Marshal.PtrToStringUTF8(result.Error) ?? "Unknown error"
: "Unknown error";
#else
string error = result.Error != IntPtr.Zero
? Native.PtrToStringUTF8(result.Error) ?? "Unknown error"
: "Unknown error";
#endif
Native.json_eval_free_result(result);
throw new JsonEvalException(error);
}
Native.json_eval_free_result(result);
}
public ValidationResult ValidateSubform(string subformPath, string data, string? context = null, bool validateReadonly = false)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException(nameof(data));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_validate_subform(_handle, subformPath, data, context, validateReadonly);
#else
var result = Native.json_eval_validate_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(data)!, Native.ToUTF8Bytes(context), validateReadonly);
#endif
return ProcessResult<ValidationResult>(result);
}
public JArray EvaluateDependentsSubform(string subformPath, string changedPath, string? data = null, string? context = null, bool reEvaluate = true, bool includeSubforms = true)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (string.IsNullOrEmpty(changedPath))
throw new ArgumentNullException(nameof(changedPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_evaluate_dependents_subform(_handle, subformPath, changedPath, data, context, reEvaluate ? 1 : 0, includeSubforms ? 1 : 0);
#else
var result = Native.json_eval_evaluate_dependents_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(changedPath)!, Native.ToUTF8Bytes(data), Native.ToUTF8Bytes(context), reEvaluate ? 1 : 0, includeSubforms ? 1 : 0);
#endif
return ProcessResultAsArray(result);
}
public string EvaluateDependentsSubformString(string subformPath, string changedPath, string? data = null, string? context = null, bool reEvaluate = true, bool includeSubforms = true)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (string.IsNullOrEmpty(changedPath))
throw new ArgumentNullException(nameof(changedPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_evaluate_dependents_subform(_handle, subformPath, changedPath, data, context, reEvaluate ? 1 : 0, includeSubforms ? 1 : 0);
#else
var result = Native.json_eval_evaluate_dependents_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(changedPath)!, Native.ToUTF8Bytes(data), Native.ToUTF8Bytes(context), reEvaluate ? 1 : 0, includeSubforms ? 1 : 0);
#endif
return ProcessResultAsString(result);
}
public JArray ResolveLayoutSubform(string subformPath, bool evaluate = false)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_resolve_layout_subform(_handle, subformPath, evaluate);
#else
var result = Native.json_eval_resolve_layout_subform(_handle, Native.ToUTF8Bytes(subformPath)!, evaluate);
#endif
return ProcessResultAsArray(result);
}
public JObject GetEvaluatedSchemaSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_evaluated_schema_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_evaluated_schema_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResult(result);
}
public JArray GetResolvedLayoutSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_resolved_layout_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_resolved_layout_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResultAsArray(result);
}
public JObject GetEvaluatedSchemaResolvedSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
return LayoutOverlayMerger.Merge(
GetEvaluatedSchemaWithoutParamsSubform(subformPath),
GetResolvedLayoutSubform(subformPath));
}
public JObject GetSchemaValueSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_schema_value_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_schema_value_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResult(result);
}
public JArray GetSchemaValueArraySubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_schema_value_array_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_schema_value_array_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResultAsArray(result);
}
public JObject GetSchemaValueObjectSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_schema_value_object_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_schema_value_object_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResult(result);
}
public JObject GetEvaluatedSchemaWithoutParamsSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_evaluated_schema_without_params_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_evaluated_schema_without_params_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResult(result);
}
public JObject? GetPlainParamsSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_plain_params_subform(_handle, subformPath);
#else
var result = Native.json_eval_get_plain_params_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
return ProcessResultNullableJObject(result);
}
public JObject? getPlainParamsSubform(string subformPath) =>
GetPlainParamsSubform(subformPath);
public JObject? GetEvaluatedParamsSubform(string subformPath, bool withStaticArray = false)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_evaluated_params_subform(_handle, subformPath, withStaticArray);
#else
var result = Native.json_eval_get_evaluated_params_subform(_handle, Native.ToUTF8Bytes(subformPath)!, withStaticArray);
#endif
return ProcessResultNullableJObject(result);
}
public JObject? getEvaluatedParamsSubform(string subformPath, bool withStaticArray = false) =>
GetEvaluatedParamsSubform(subformPath, withStaticArray);
public JObject? GetEvaluatedSchemaByPathSubform(string subformPath, string schemaPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (string.IsNullOrEmpty(schemaPath))
throw new ArgumentNullException(nameof(schemaPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_evaluated_schema_by_path_subform(_handle, subformPath, schemaPath);
#else
var result = Native.json_eval_get_evaluated_schema_by_path_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(schemaPath)!);
#endif
try
{
if (!result.Success)
{
return null;
}
if (result.DataPtr == IntPtr.Zero)
return null;
int dataLen = (int)result.DataLen.ToUInt32();
if (dataLen == 0)
return null;
byte[] buffer = new byte[dataLen];
Marshal.Copy(result.DataPtr, buffer, 0, dataLen);
string json = Encoding.UTF8.GetString(buffer);
return JObject.Parse(json);
}
finally
{
Native.json_eval_free_result(result);
}
}
public JToken GetEvaluatedSchemaByPathsSubform(string subformPath, string[] schemaPaths, ReturnFormat format = ReturnFormat.Nested)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (schemaPaths == null || schemaPaths.Length == 0)
throw new ArgumentNullException(nameof(schemaPaths));
string pathsJson = JsonConvert.SerializeObject(schemaPaths);
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_evaluated_schema_by_paths_subform(_handle, subformPath, pathsJson, (byte)format);
#else
var result = Native.json_eval_get_evaluated_schema_by_paths_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(pathsJson)!, (byte)format);
#endif
if (!result.Success)
{
#if NETCOREAPP || NET5_0_OR_GREATER
string error = result.Error != IntPtr.Zero
? Marshal.PtrToStringUTF8(result.Error) ?? "Unknown error"
: "Unknown error";
#else
string error = result.Error != IntPtr.Zero
? Native.PtrToStringUTF8(result.Error) ?? "Unknown error"
: "Unknown error";
#endif
Native.json_eval_free_result(result);
throw new InvalidOperationException($"Failed to get evaluated schema by paths from subform: {error}");
}
try
{
if (result.DataPtr == IntPtr.Zero)
return format == ReturnFormat.Array ? (JToken)new JArray() : (JToken)new JObject();
int dataLen = (int)result.DataLen.ToUInt32();
if (dataLen == 0)
return format == ReturnFormat.Array ? (JToken)new JArray() : (JToken)new JObject();
byte[] buffer = new byte[dataLen];
Marshal.Copy(result.DataPtr, buffer, 0, dataLen);
string json = Encoding.UTF8.GetString(buffer);
return format == ReturnFormat.Array ? (JToken)JArray.Parse(json) : (JToken)JObject.Parse(json);
}
finally
{
Native.json_eval_free_result(result);
}
}
public List<string> GetSubformPaths()
{
ThrowIfDisposed();
var result = Native.json_eval_get_subform_paths(_handle);
var array = ProcessResultAsArray(result);
return array.ToObject<List<string>>() ?? new List<string>();
}
public bool HasSubform(string subformPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_has_subform(_handle, subformPath);
#else
var result = Native.json_eval_has_subform(_handle, Native.ToUTF8Bytes(subformPath)!);
#endif
try
{
if (!result.Success)
return false;
if (result.DataPtr == IntPtr.Zero)
return false;
int dataLen = (int)result.DataLen.ToUInt32();
if (dataLen == 0)
return false;
byte[] buffer = new byte[dataLen];
Marshal.Copy(result.DataPtr, buffer, 0, dataLen);
string value = Encoding.UTF8.GetString(buffer);
return value == "true";
}
finally
{
Native.json_eval_free_result(result);
}
}
public JObject? GetSchemaByPathSubform(string subformPath, string schemaPath)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (string.IsNullOrEmpty(schemaPath))
throw new ArgumentNullException(nameof(schemaPath));
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_schema_by_path_subform(_handle, subformPath, schemaPath);
#else
var result = Native.json_eval_get_schema_by_path_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(schemaPath)!);
#endif
try
{
if (!result.Success)
{
return null;
}
if (result.DataPtr == IntPtr.Zero)
return null;
int dataLen = (int)result.DataLen.ToUInt32();
if (dataLen == 0)
return null;
byte[] buffer = new byte[dataLen];
Marshal.Copy(result.DataPtr, buffer, 0, dataLen);
string json = Encoding.UTF8.GetString(buffer);
return JObject.Parse(json);
}
finally
{
Native.json_eval_free_result(result);
}
}
public JToken GetSchemaByPathsSubform(string subformPath, string[] schemaPaths, ReturnFormat format = ReturnFormat.Nested)
{
ThrowIfDisposed();
if (string.IsNullOrEmpty(subformPath))
throw new ArgumentNullException(nameof(subformPath));
if (schemaPaths == null || schemaPaths.Length == 0)
throw new ArgumentNullException(nameof(schemaPaths));
string pathsJson = JsonConvert.SerializeObject(schemaPaths);
#if NETCOREAPP || NET5_0_OR_GREATER
var result = Native.json_eval_get_schema_by_paths_subform(_handle, subformPath, pathsJson, (byte)format);
#else
var result = Native.json_eval_get_schema_by_paths_subform(_handle, Native.ToUTF8Bytes(subformPath)!, Native.ToUTF8Bytes(pathsJson)!, (byte)format);
#endif
if (!result.Success)
{
#if NETCOREAPP || NET5_0_OR_GREATER
string error = result.Error != IntPtr.Zero
? Marshal.PtrToStringUTF8(result.Error) ?? "Unknown error"
: "Unknown error";
#else
string error = result.Error != IntPtr.Zero
? Native.PtrToStringUTF8(result.Error) ?? "Unknown error"
: "Unknown error";
#endif
Native.json_eval_free_result(result);
throw new InvalidOperationException($"Failed to get schema by paths from subform: {error}");
}
try
{
if (result.DataPtr == IntPtr.Zero)
return format == ReturnFormat.Array ? (JToken)new JArray() : (JToken)new JObject();
int dataLen = (int)result.DataLen.ToUInt32();
if (dataLen == 0)
return format == ReturnFormat.Array ? (JToken)new JArray() : (JToken)new JObject();
byte[] buffer = new byte[dataLen];
Marshal.Copy(result.DataPtr, buffer, 0, dataLen);
string json = Encoding.UTF8.GetString(buffer);
return format == ReturnFormat.Array ? (JToken)JArray.Parse(json) : (JToken)JObject.Parse(json);
}
finally
{
Native.json_eval_free_result(result);
}
}
}
}