UNPKG

@graphql-codegen/c-sharp-common

Version:
35 lines (34 loc) 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonAttributesSourceConfiguration = void 0; exports.getJsonAttributeSourceConfiguration = getJsonAttributeSourceConfiguration; function unsupportedSource(attributesSource) { throw new Error(`Unsupported JSON attributes source: ${attributesSource}`); } class JsonAttributesSourceConfiguration { constructor(namespace, propertyAttribute, requiredAttribute, enumConfig) { this.namespace = namespace; this.propertyAttribute = propertyAttribute; this.requiredAttribute = requiredAttribute; this.enumConfiguration = enumConfig; } } exports.JsonAttributesSourceConfiguration = JsonAttributesSourceConfiguration; const newtonsoftConfiguration = new JsonAttributesSourceConfiguration('Newtonsoft.Json', 'JsonProperty', 'JsonRequired', { decorator: '[System.Runtime.Serialization.DataContract]\n[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]', enumMemberAttribute: value => `[System.Runtime.Serialization.EnumMember(Value = "${value}")]`, }); // System.Text.Json does not have support of `JsonRequired` alternative (as for .NET 5) const systemTextJsonConfiguration = new JsonAttributesSourceConfiguration('System.Text.Json.Serialization', 'JsonPropertyName', null, { enumMemberAttribute: value => `[JsonStringEnumMemberName("${value}")]`, decorator: '[JsonConverter(typeof(JsonStringEnumConverter))]', }); function getJsonAttributeSourceConfiguration(attributesSource) { switch (attributesSource) { case 'Newtonsoft.Json': return newtonsoftConfiguration; case 'System.Text.Json': return systemTextJsonConfiguration; } unsupportedSource(attributesSource); }