// this file was generated by a tool 
{%- if not uses_protobuf -%}{%- exit -%}{%- endif -%}
{%- set schema_name = class_name -%}
{%- set schema = root %}
using System;

{%- macro emitclass(type, type_name) %}

    public partial class {{ type_name }}
    {
        public static {{ type_name }} FromData(object? data, string? contentType)
        {
            // generated from a JSON schema and therefore only supporting JSON
            if ( data is {{ type_name }})
            {
                return ({{ type_name }})data;
            }
            var stream = data as Stream;
            if (stream == null)
            {
                if (data is BinaryData)
                {
                    stream = ((BinaryData)data).ToStream();
                }
                else if ( data is byte[])
                {
                    stream = new MemoryStream((byte[])data);
                }
                else
                {
                    throw new NotSupportedException("Data is not of a supported type for conversion to {{ type_name }}");  
                }
            }
            return {{ type_name }}.Parser.ParseFrom(stream);
        }

        public byte[] ToByteArray(string contentType)
        {
            if ( contentType == "application/protobuf" )
            {
                var memoryStream = new MemoryStream();
                using (var output = new Google.Protobuf.CodedOutputStream(memoryStream))
                {
                    this.WriteTo(output);
                    output.Flush();
                    return memoryStream.ToArray();
                }
            }
            throw new NotSupportedException("Content type not supported for serialization to byte array: " + contentType);
        }
    }
{%- endmacro %}

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace {{ schema_name | namespace(project_name) | pascal }}
{
    {{- emitclass( schema, schema_name | strip_namespace ) }}
}