JsonSchema
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace System.Text.Json.Schema
{
internal sealed class JsonSchema
{
internal const string RefPropertyName = "$ref";
internal const string CommentPropertyName = "$comment";
internal const string TypePropertyName = "type";
internal const string FormatPropertyName = "format";
internal const string PatternPropertyName = "pattern";
internal const string PropertiesPropertyName = "properties";
internal const string RequiredPropertyName = "required";
internal const string ItemsPropertyName = "items";
internal const string AdditionalPropertiesPropertyName = "additionalProperties";
internal const string EnumPropertyName = "enum";
internal const string NotPropertyName = "not";
internal const string AnyOfPropertyName = "anyOf";
internal const string ConstPropertyName = "const";
internal const string DefaultPropertyName = "default";
internal const string MinLengthPropertyName = "minLength";
internal const string MaxLengthPropertyName = "maxLength";
private readonly bool? _trueOrFalse;
private string _ref;
private string _comment;
private JsonSchemaType _type;
private string _format;
private string _pattern;
private JsonNode _constant;
private List<KeyValuePair<string, JsonSchema>> _properties;
private List<string> _required;
private JsonSchema _items;
private JsonSchema _additionalProperties;
private JsonArray _enum;
private JsonSchema _not;
private List<JsonSchema> _anyOf;
private bool _hasDefaultValue;
private JsonNode _defaultValue;
private int? _minLength;
private int? _maxLength;
public bool IsTrue => _trueOrFalse ?? false;
public bool IsFalse => (!_trueOrFalse) ?? false;
public string Ref {
get {
return _ref;
}
set {
VerifyMutable();
_ref = value;
}
}
public string Comment {
get {
return _comment;
}
set {
VerifyMutable();
_comment = value;
}
}
public JsonSchemaType Type {
get {
return _type;
}
set {
VerifyMutable();
_type = value;
}
}
public string Format {
get {
return _format;
}
set {
VerifyMutable();
_format = value;
}
}
public string Pattern {
get {
return _pattern;
}
set {
VerifyMutable();
_pattern = value;
}
}
public JsonNode Constant {
get {
return _constant;
}
set {
VerifyMutable();
_constant = value;
}
}
public List<KeyValuePair<string, JsonSchema>> Properties {
get {
return _properties;
}
set {
VerifyMutable();
_properties = value;
}
}
public List<string> Required {
get {
return _required;
}
set {
VerifyMutable();
_required = value;
}
}
public JsonSchema Items {
get {
return _items;
}
set {
VerifyMutable();
_items = value;
}
}
public JsonSchema AdditionalProperties {
get {
return _additionalProperties;
}
set {
VerifyMutable();
_additionalProperties = value;
}
}
public JsonArray Enum {
get {
return _enum;
}
set {
VerifyMutable();
_enum = value;
}
}
public JsonSchema Not {
get {
return _not;
}
set {
VerifyMutable();
_not = value;
}
}
public List<JsonSchema> AnyOf {
get {
return _anyOf;
}
set {
VerifyMutable();
_anyOf = value;
}
}
public bool HasDefaultValue {
get {
return _hasDefaultValue;
}
set {
VerifyMutable();
_hasDefaultValue = value;
}
}
public JsonNode DefaultValue {
get {
return _defaultValue;
}
set {
VerifyMutable();
_defaultValue = value;
}
}
public int? MinLength {
get {
return _minLength;
}
set {
VerifyMutable();
_minLength = value;
}
}
public int? MaxLength {
get {
return _maxLength;
}
set {
VerifyMutable();
_maxLength = value;
}
}
public JsonSchemaExporterContext? ExporterContext { get; set; }
public int KeywordCount {
get {
if (_trueOrFalse.HasValue)
return 0;
<>c__DisplayClass98_0 <>c__DisplayClass98_ = default(<>c__DisplayClass98_0);
<>c__DisplayClass98_.count = 0;
<get_KeywordCount>g__Count|98_0(Ref != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Comment != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Type != JsonSchemaType.Any, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Format != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Pattern != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Constant != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Properties != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Required != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Items != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(AdditionalProperties != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Enum != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(Not != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(AnyOf != null, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(HasDefaultValue, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(MinLength.HasValue, ref <>c__DisplayClass98_);
<get_KeywordCount>g__Count|98_0(MaxLength.HasValue, ref <>c__DisplayClass98_);
return <>c__DisplayClass98_.count;
}
}
private static ReadOnlySpan<JsonSchemaType> s_schemaValues {
get {
object obj = global::<PrivateImplementationDetails>.BC308CC8478F56D6B93E287CF1CADBE6F297C5AF89914899F8ACB21206B84FC2_A6;
if (obj == null) {
obj = new int[7] {
16,
4,
8,
2,
64,
32,
1
};
global::<PrivateImplementationDetails>.BC308CC8478F56D6B93E287CF1CADBE6F297C5AF89914899F8ACB21206B84FC2_A6 = (int[])obj;
}
return new ReadOnlySpan<JsonSchemaType>((JsonSchemaType[])obj);
}
}
public static JsonSchema CreateFalseSchema()
{
return new JsonSchema(false);
}
public static JsonSchema CreateTrueSchema()
{
return new JsonSchema(true);
}
public JsonSchema()
{
}
private JsonSchema(bool trueOrFalse)
{
_trueOrFalse = trueOrFalse;
}
public void MakeNullable()
{
if (!_trueOrFalse.HasValue && Type != 0)
Type |= JsonSchemaType.Null;
}
public JsonNode ToJsonNode(JsonSchemaExporterOptions options)
{
<>c__DisplayClass100_0 <>c__DisplayClass100_ = default(<>c__DisplayClass100_0);
<>c__DisplayClass100_.<>4__this = this;
<>c__DisplayClass100_.options = options;
bool? trueOrFalse = _trueOrFalse;
if (trueOrFalse.HasValue) {
bool valueOrDefault = trueOrFalse.GetValueOrDefault();
return <ToJsonNode>g__CompleteSchema|100_0(valueOrDefault, ref <>c__DisplayClass100_);
}
JsonObject jsonObject = new JsonObject((JsonNodeOptions?)null);
if (Ref != null)
jsonObject.Add("$ref", Ref);
if (Comment != null)
jsonObject.Add("$comment", Comment);
JsonNode jsonNode = MapSchemaType(Type);
if (jsonNode != null)
jsonObject.Add("type", jsonNode);
if (Format != null)
jsonObject.Add("format", Format);
if (Pattern != null)
jsonObject.Add("pattern", Pattern);
if (Constant != null)
jsonObject.Add("const", Constant);
if (Properties != null) {
JsonObject jsonObject2 = new JsonObject((JsonNodeOptions?)null);
foreach (KeyValuePair<string, JsonSchema> property in Properties) {
jsonObject2.Add(property.Key, property.Value.ToJsonNode(<>c__DisplayClass100_.options));
}
jsonObject.Add("properties", jsonObject2);
}
if (Required != null) {
JsonArray jsonArray = new JsonArray((JsonNodeOptions?)null);
foreach (string item in Required) {
jsonArray.Add((JsonNode)item);
}
jsonObject.Add("required", jsonArray);
}
if (Items != null)
jsonObject.Add("items", Items.ToJsonNode(<>c__DisplayClass100_.options));
if (AdditionalProperties != null)
jsonObject.Add("additionalProperties", AdditionalProperties.ToJsonNode(<>c__DisplayClass100_.options));
if (Enum != null)
jsonObject.Add("enum", Enum);
if (Not != null)
jsonObject.Add("not", Not.ToJsonNode(<>c__DisplayClass100_.options));
if (AnyOf != null) {
JsonArray jsonArray2 = new JsonArray((JsonNodeOptions?)null);
foreach (JsonSchema item2 in AnyOf) {
jsonArray2.Add(item2.ToJsonNode(<>c__DisplayClass100_.options));
}
jsonObject.Add("anyOf", jsonArray2);
}
if (HasDefaultValue)
jsonObject.Add("default", DefaultValue);
int? minLength = MinLength;
if (minLength.HasValue) {
int valueOrDefault2 = minLength.GetValueOrDefault();
jsonObject.Add("minLength", valueOrDefault2);
}
minLength = MaxLength;
if (minLength.HasValue) {
int valueOrDefault3 = minLength.GetValueOrDefault();
jsonObject.Add("maxLength", valueOrDefault3);
}
return <ToJsonNode>g__CompleteSchema|100_0(jsonObject, ref <>c__DisplayClass100_);
}
public static void EnsureMutable(ref JsonSchema schema)
{
bool? trueOrFalse = schema._trueOrFalse;
if (trueOrFalse.HasValue) {
if (!trueOrFalse.GetValueOrDefault())
schema = new JsonSchema {
Not = CreateTrueSchema()
};
else
schema = new JsonSchema();
}
}
private void VerifyMutable()
{
if (_trueOrFalse.HasValue)
<VerifyMutable>g__Throw|104_0();
}
public static JsonNode MapSchemaType(JsonSchemaType schemaType)
{
if (schemaType == JsonSchemaType.Any)
return null;
string text = <MapSchemaType>g__ToIdentifier|105_0(schemaType);
if (text != null)
return text;
JsonArray jsonArray = new JsonArray((JsonNodeOptions?)null);
ReadOnlySpan<JsonSchemaType> s_schemaValues = JsonSchema.s_schemaValues;
foreach (JsonSchemaType jsonSchemaType in s_schemaValues) {
if ((schemaType & jsonSchemaType) != 0)
jsonArray.Add((JsonNode)<MapSchemaType>g__ToIdentifier|105_0(jsonSchemaType));
}
return jsonArray;
}
}
}