// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
namespace EpicGames.Core
{
///
/// Exception thrown for errors parsing JSON files
///
public class JsonParseException : Exception
{
///
/// Constructor
///
/// Format string
/// Optional arguments
public JsonParseException(string Format, params object[] Args)
: base(String.Format(Format, Args))
{
}
}
///
/// Stores a JSON object in memory
///
public class JsonObject
{
Dictionary RawObject;
///
/// Construct a JSON object from the raw string -> object dictionary
///
/// Raw object parsed from disk
public JsonObject(Dictionary InRawObject)
{
RawObject = new Dictionary(InRawObject, StringComparer.InvariantCultureIgnoreCase);
}
///
/// Constructor
///
///
public JsonObject(JsonElement Element)
{
RawObject = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
foreach (JsonProperty Property in Element.EnumerateObject())
{
RawObject[Property.Name] = ParseElement(Property.Value);
}
}
///
/// Parse an individual element
///
///
///
public static object? ParseElement(JsonElement Element)
{
switch(Element.ValueKind)
{
case JsonValueKind.Array:
return Element.EnumerateArray().Select(x => ParseElement(x)).ToArray();
case JsonValueKind.Number:
return Element.GetDouble();
case JsonValueKind.Object:
return Element.EnumerateObject().ToDictionary(x => x.Name, x => ParseElement(x.Value));
case JsonValueKind.String:
return Element.GetString();
case JsonValueKind.False:
return false;
case JsonValueKind.True:
return true;
case JsonValueKind.Null:
return null;
default:
throw new NotImplementedException();
}
}
///
/// Read a JSON file from disk and construct a JsonObject from it
///
/// File to read from
/// New JsonObject instance
public static JsonObject Read(FileReference File)
{
string Text = FileReference.ReadAllText(File);
try
{
return Parse(Text);
}
catch(Exception Ex)
{
throw new JsonParseException("Unable to parse {0}: {1}", File, Ex.Message);
}
}
///
/// Tries to read a JSON file from disk
///
/// File to read from
/// On success, receives the parsed object
/// True if the file was read, false otherwise
public static bool TryRead(FileReference FileName, [NotNullWhen(true)] out JsonObject? Result)
{
if (!FileReference.Exists(FileName))
{
Result = null;
return false;
}
string Text = FileReference.ReadAllText(FileName);
return TryParse(Text, out Result);
}
///
/// Parse a JsonObject from the given raw text string
///
/// The text to parse
/// New JsonObject instance
public static JsonObject Parse(string Text)
{
JsonDocument Document = JsonDocument.Parse(Text, new JsonDocumentOptions { AllowTrailingCommas = true });
return new JsonObject(Document.RootElement);
}
///
/// Try to parse a JsonObject from the given raw text string
///
/// The text to parse
/// On success, receives the new JsonObject
/// True if the object was parsed
public static bool TryParse(string Text, [NotNullWhen(true)] out JsonObject? Result)
{
try
{
Result = Parse(Text);
return true;
}
catch (Exception)
{
Result = null;
return false;
}
}
///
/// List of key names in this object
///
public IEnumerable KeyNames
{
get { return RawObject.Keys; }
}
///
/// Gets a string field by the given name from the object, throwing an exception if it is not there or cannot be parsed.
///
/// Name of the field to get
/// The field value
public string GetStringField(string FieldName)
{
string? StringValue;
if (!TryGetStringField(FieldName, out StringValue))
{
throw new JsonParseException("Missing or invalid '{0}' field", FieldName);
}
return StringValue;
}
///
/// Tries to read a string field by the given name from the object
///
/// Name of the field to get
/// On success, receives the field value
/// True if the field could be read, false otherwise
public bool TryGetStringField(string FieldName, [NotNullWhen(true)] out string? Result)
{
object? RawValue;
if (RawObject.TryGetValue(FieldName, out RawValue) && (RawValue is string))
{
Result = (string)RawValue;
return true;
}
else
{
Result = null;
return false;
}
}
///
/// Gets a string array field by the given name from the object, throwing an exception if it is not there or cannot be parsed.
///
/// Name of the field to get
/// The field value
public string[] GetStringArrayField(string FieldName)
{
string[]? StringValues;
if (!TryGetStringArrayField(FieldName, out StringValues))
{
throw new JsonParseException("Missing or invalid '{0}' field", FieldName);
}
return StringValues;
}
///
/// Tries to read a string array field by the given name from the object
///
/// Name of the field to get
/// On success, receives the field value
/// True if the field could be read, false otherwise
public bool TryGetStringArrayField(string FieldName, [NotNullWhen(true)] out string[]? Result)
{
object? RawValue;
if (RawObject.TryGetValue(FieldName, out RawValue) && (RawValue is IEnumerable