|
1 |
| - {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation %} |
| 1 | + {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %} |
2 | 2 | /// <summary>
|
3 | 3 | /// Creates an object from the data
|
4 | 4 | /// </summary>
|
|
11 | 11 | if ( data is {{ class_name }}) return ({{ class_name }})data;
|
12 | 12 | if ( contentTypeString == null ) contentTypeString = System.Net.Mime.MediaTypeNames.Application.Octet;
|
13 | 13 | var contentType = new System.Net.Mime.ContentType(contentTypeString);
|
14 |
| - {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation %} |
| 14 | + {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %} |
15 | 15 | if ( contentType.MediaType.EndsWith("+gzip"))
|
16 | 16 | {
|
17 | 17 | var stream = data switch
|
|
87 | 87 | return ((System.BinaryData)data).ToObjectFromJson<{{ class_name }}>();
|
88 | 88 | }
|
89 | 89 | }
|
| 90 | + {%- endif %} |
| 91 | + {%- if system_xml_annotation %} |
| 92 | + if ( contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Text.Xml) || contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Xml) || contentType.MediaType.EndsWith("+xml")) |
| 93 | + { |
| 94 | + var serializer = new System.Xml.Serialization.XmlSerializer(typeof({{ class_name }})); |
| 95 | + if (data is string) |
| 96 | + { |
| 97 | + using (var reader = new System.IO.StringReader((string)data)) |
| 98 | + { |
| 99 | + return ({{ class_name }}?)serializer.Deserialize(reader); |
| 100 | + } |
| 101 | + } |
| 102 | + else if (data is System.IO.Stream) |
| 103 | + { |
| 104 | + return ({{ class_name }}?)serializer.Deserialize((System.IO.Stream)data); |
| 105 | + } |
| 106 | + else if (data is System.BinaryData) |
| 107 | + { |
| 108 | + var memoryStream = new System.IO.MemoryStream(((System.BinaryData)data).ToArray()); |
| 109 | + return ({{ class_name }}?)serializer.Deserialize(memoryStream); |
| 110 | + } |
| 111 | + else if (data is byte[]) |
| 112 | + { |
| 113 | + var memoryStream = new System.IO.MemoryStream((byte[])data); |
| 114 | + return ({{ class_name }}?)serializer.Deserialize(memoryStream); |
| 115 | + } |
| 116 | + } |
90 | 117 | {%- endif %}
|
91 | 118 | throw new System.NotSupportedException($"Unsupported media type {contentType.MediaType}");
|
92 | 119 | }
|
93 |
| - {%-endif %} |
| 120 | + {%- endif %} |
94 | 121 |
|
95 | 122 | {%- if avro_annotation %}
|
96 | 123 | private class SpecificDatumWriter : global::Avro.Specific.SpecificDatumWriter<{{ class_name }}>
|
|
101 | 128 |
|
102 | 129 | protected override WriteItem ResolveEnum(global::Avro.EnumSchema es)
|
103 | 130 | {
|
104 |
| - return base.ResolveEnum(global::Avro.EnumSchema.Create(es.Name, es.Symbols, GetType().Assembly.GetName().Name+"."+es.Namespace, null, null, es.Documentation, es.Default)); |
| 131 | + var enumType = GetType().Assembly.GetType(GetType().Assembly.GetName().Name+"."+es.Namespace + "." + es.Name, false, true); |
| 132 | + if (enumType != null) |
| 133 | + { |
| 134 | + return base.ResolveEnum(global::Avro.EnumSchema.Create(enumType.Name, es.Symbols, enumType.Namespace, null, null, es.Documentation, es.Default)); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + return base.ResolveEnum(es); |
| 139 | + } |
105 | 140 | }
|
106 | 141 | }
|
107 | 142 | {%- endif %}
|
108 | 143 |
|
109 | 144 | {%- if avro_annotation %}
|
110 | 145 | {%- endif%}
|
111 | 146 |
|
112 |
| - {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation %} |
| 147 | + {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %} |
113 | 148 | /// <summary>
|
114 | 149 | /// Converts the object to a byte array
|
115 | 150 | /// </summary>
|
|
151 | 186 | result = System.Text.Encoding.GetEncoding(contentType.CharSet??"utf-8").GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
|
152 | 187 | }
|
153 | 188 | {%- endif %}
|
154 |
| - {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation %} |
| 189 | + {%- if system_xml_annotation %} |
| 190 | + if (contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Text.Xml) || contentType.MediaType.StartsWith(System.Net.Mime.MediaTypeNames.Application.Xml) || contentType.MediaType.EndsWith("+xml")) |
| 191 | + { |
| 192 | + var serializer = new System.Xml.Serialization.XmlSerializer(typeof({{ class_name }})); |
| 193 | + using (var stream = new System.IO.MemoryStream()) |
| 194 | + { |
| 195 | + using (var writer = new System.IO.StreamWriter(stream)) |
| 196 | + { |
| 197 | + serializer.Serialize(writer, this); |
| 198 | + writer.Flush(); |
| 199 | + result = stream.ToArray(); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + {%- endif %} |
| 204 | + {%- if avro_annotation or system_text_json_annotation or newtonsoft_json_annotation or system_xml_annotation %} |
155 | 205 | if (result != null && contentType.MediaType.EndsWith("+gzip"))
|
156 | 206 | {
|
157 | 207 | var stream = new System.IO.MemoryStream();
|
|
166 | 216 | }
|
167 | 217 | {%- endif %}
|
168 | 218 |
|
169 |
| - {%- if system_text_json_annotation %} |
| 219 | + {%- if system_text_json_annotation or newtonsoft_json_annotation %} |
170 | 220 | /// <summary>
|
171 | 221 | /// Checks if the JSON element matches the schema
|
172 | 222 | /// </summary>
|
|
0 commit comments