-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONExportationOptions.cs
More file actions
303 lines (279 loc) · 13.3 KB
/
JSONExportationOptions.cs
File metadata and controls
303 lines (279 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace MetadataChange
{
/// <summary>
/// Remove byte arrays when serializing the JSON
/// </summary>
/// <param name="AdvancedMetadata">What is being exported</param>
public class IgnoreByteArrayResolver(MetadataExportType AdvancedMetadata) : DefaultContractResolver
{
protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
// We'll have a lot of if switches here, but we just need to change the Converter to include metadata in various tags. And also, we need to remove byte array references.
if (property.PropertyName == "FrameId" || (property.PropertyName == "BoxType" && property.PropertyType == typeof(TagLib.ByteVector)))
{
property.Converter = new ByteVectorToStringConverter();
}
else if (property.PropertyName == "BoxType" && property.PropertyType == typeof(TagLib.ByteVector[]))
{
property.Converter = new ByteVectorDoubleArrayToStringConverter();
}
else if (property.PropertyType == typeof(IEnumerable<TagLib.Ogg.XiphComment>))
{
property.Converter = new XiphCommentConverter(true);
}
else if (property.PropertyType == typeof(TagLib.Asf.Tag))
{
property.Converter = new AsfCommentConverter();
}
else if (property.PropertyType == typeof(Dictionary<string, List<TagLib.Matroska.SimpleTag>>))
{
property.Converter = new MatroskaSimpleTagConverter();
}
else if (property.PropertyType == typeof(byte[]) || property.PropertyType == typeof(TagLib.ByteVector) || (AdvancedMetadata != MetadataExportType.ALL_APP && property.PropertyType == typeof(TagLib.NonContainer.StartTag)) || (AdvancedMetadata != MetadataExportType.ALL_APP && property.PropertyType == typeof(TagLib.NonContainer.EndTag) || (AdvancedMetadata == MetadataExportType.COMMON && property.PropertyType == typeof(TagLib.Tag[]))))
{
property.ShouldSerialize = _ => false;
}
else if (property.PropertyType != null && !property.PropertyType.IsPrimitive && property.PropertyType != typeof(string))
{
property.Converter = null; // Prevent Newtonsoft from caching wrong converter
}
return property;
}
}
/// <summary>
/// Only fixes serialization of some TagLib libraries. This is done also by the other two ContractResolvers. Without this, metadata values of certain tags might be lost.
/// It also deletes StartTag and EndTag if not required.
/// </summary>
/// <param name="AdvancedMetadata">What is being exported</param>
public class StandardJsonResolver(MetadataExportType AdvancedMetadata) : DefaultContractResolver
{
protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.PropertyName == "FrameId" || (property.PropertyName == "BoxType" && property.PropertyType == typeof(TagLib.ByteVector)))
{
property.Converter = new ByteVectorToStringConverter();
}
else if (property.PropertyName == "BoxType" && property.PropertyType == typeof(TagLib.ByteVector[]))
{
property.Converter = new ByteVectorDoubleArrayToStringConverter();
}
else if (property.PropertyType == typeof(IEnumerable<TagLib.Ogg.XiphComment>))
{
property.Converter = new XiphCommentConverter(false);
}
else if (property.PropertyType == typeof(Dictionary<string, List<TagLib.Matroska.SimpleTag>>))
{
property.Converter = new MatroskaSimpleTagConverter();
}
else if (property.PropertyType == typeof(TagLib.Asf.Tag))
{
property.Converter = new AsfCommentConverter();
}
else if (AdvancedMetadata != MetadataExportType.ALL_APP && (property.PropertyType == typeof(TagLib.NonContainer.StartTag) || property.PropertyType == typeof(TagLib.NonContainer.EndTag)) || (AdvancedMetadata == MetadataExportType.COMMON && property.PropertyType == typeof(TagLib.Tag[])))
{
property.ShouldSerialize = _ => false;
}
return property;
}
}
/// <summary>
/// Convert byte arrays when serializing the JSON
/// </summary>
/// <param name="AdvancedMetadata">What is being exported</param>
public class ByteArrayBase64Resolver(MetadataExportType AdvancedMetadata) : DefaultContractResolver
{
protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
Console.WriteLine(property.PropertyName);
Console.WriteLine(property.PropertyType);
if (property.PropertyName == "FrameId" || (property.PropertyName == "BoxType" && property.PropertyType == typeof(TagLib.ByteVector)))
{
property.Converter = new ByteVectorToStringConverter();
}
else if (property.PropertyName == "BoxType" && property.PropertyType == typeof(TagLib.ByteVector[]))
{
property.Converter = new ByteVectorDoubleArrayToStringConverter();
}
else if (property.PropertyType == typeof(IEnumerable<TagLib.Ogg.XiphComment>))
{
property.Converter = new XiphCommentConverter(false);
}
else if (property.PropertyType == typeof(Dictionary<string, List<TagLib.Matroska.SimpleTag>>))
{
property.Converter = new MatroskaSimpleTagConverter();
}
else if (property.PropertyType == typeof(TagLib.Asf.Tag))
{
property.Converter = new AsfCommentConverter();
}
else if (AdvancedMetadata != MetadataExportType.ALL_APP && (property.PropertyType == typeof(TagLib.NonContainer.StartTag) || property.PropertyType == typeof(TagLib.NonContainer.EndTag)) || (AdvancedMetadata == MetadataExportType.COMMON && property.PropertyType == typeof(TagLib.Tag[])))
{
property.ShouldSerialize = _ => false;
}
if (property.PropertyType == typeof(byte[]))
{
property.Converter = new ByteArrayToBase64Converter();
}
else if (property.PropertyType == typeof(TagLib.ByteVector))
{
property.Converter = new TagLibByteVectorToBase64Converter();
}
return property;
}
}
/// <summary>
/// Convert a Byte Array to a base64
/// </summary>
public class ByteArrayToBase64Converter : JsonConverter<byte[]>
{
public override void WriteJson(JsonWriter writer, byte[] value, JsonSerializer serializer)
{
writer.WriteValue(Convert.ToBase64String(value));
}
public override byte[] ReadJson(JsonReader reader, Type objectType, byte[] existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return [];
}
}
/// <summary>
/// Decode a byte vector to a string
/// </summary>
public class ByteVectorToStringConverter : JsonConverter<TagLib.ByteVector>
{
public override void WriteJson(JsonWriter writer, TagLib.ByteVector value, JsonSerializer serializer)
{
writer.WriteValue(Encoding.UTF8.GetString(value.ToArray()));
}
public override TagLib.ByteVector ReadJson(JsonReader reader, Type objectType, TagLib.ByteVector existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return [];
}
}
/// <summary>
/// Convert an array of ByteVectors to a String
/// </summary>
public class ByteVectorDoubleArrayToStringConverter : JsonConverter<TagLib.ByteVector[]>
{
public override void WriteJson(JsonWriter writer, TagLib.ByteVector[] value, JsonSerializer serializer)
{
string[] output = value.Select(i => Encoding.UTF8.GetString(i.ToArray())).ToArray();
writer.WriteValue(output);
}
public override TagLib.ByteVector[] ReadJson(JsonReader reader, Type objectType, TagLib.ByteVector[] existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return [[]];
}
}
/// <summary>
/// Convert a Matroska SimpleTag to a serializable Dictionary of keys and values.
/// In case subkeys are there, the key will be a JSON-stringified array with the tag and the subtag.
/// </summary>
public class MatroskaSimpleTagConverter : JsonConverter<Dictionary<String, List<TagLib.Matroska.SimpleTag>>>
{
public override void WriteJson(JsonWriter writer, Dictionary<String, List<TagLib.Matroska.SimpleTag>> value, JsonSerializer serializer)
{
Dictionary<string, string> output = [];
foreach (var singleTag in value)
{
foreach (var tag in singleTag.Value)
{
if (tag.SimpleTags == null) output[singleTag.Key] = Encoding.UTF8.GetString(tag.Value.ToArray()); // Simple key/value metadata
else
{
foreach (var innerTags in tag.SimpleTags) // Metadata with key, subkey, and value
{
output[Newtonsoft.Json.JsonConvert.SerializeObject(new string[] { singleTag.Key, innerTags.Key })] = Encoding.UTF8.GetString(innerTags.Value.First().Value.ToArray());
}
}
}
}
serializer.Serialize(writer, output);
}
public override Dictionary<string, List<TagLib.Matroska.SimpleTag>> ReadJson(JsonReader reader, Type objectType, Dictionary<String, List<TagLib.Matroska.SimpleTag>> existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return [];
}
}
/// <summary>
/// Convert Xiph comments to a serializable dictionary
/// </summary>
/// <param name="noBinary"></param>
public class XiphCommentConverter(bool noBinary) : JsonConverter<IEnumerable<TagLib.Ogg.XiphComment>>
{
public override void WriteJson(JsonWriter writer, IEnumerable<TagLib.Ogg.XiphComment> value, JsonSerializer serializer)
{
Dictionary<string, string[]> output = [];
foreach (TagLib.Ogg.XiphComment comment in value)
{
foreach (var key in comment)
{
if (key == "METADATA_BLOCK_PICTURE" && noBinary) continue;
output[key] = comment.GetField(key);
}
}
serializer.Serialize(writer, output);
}
public override IEnumerable<TagLib.Ogg.XiphComment> ReadJson(JsonReader reader, Type objectType, IEnumerable<TagLib.Ogg.XiphComment> existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return [];
}
}
/// <summary>
/// Convert ASF tags in a serializable dictionarues
/// </summary>
public class AsfCommentConverter : JsonConverter<TagLib.Asf.Tag>
{
public override void WriteJson(JsonWriter writer, TagLib.Asf.Tag value, JsonSerializer serializer)
{
Dictionary<string, string> output = [];
foreach (var comment in value) output[comment.Name] = comment.ToString();
serializer.Serialize(writer, output);
}
public override TagLib.Asf.Tag ReadJson(JsonReader reader, Type objectType, TagLib.Asf.Tag existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return null;
}
}
/// <summary>
/// Convert a TagLibVector to a Base64 string
/// </summary>
public class TagLibByteVectorToBase64Converter : JsonConverter<TagLib.ByteVector>
{
public override void WriteJson(JsonWriter writer, TagLib.ByteVector value, JsonSerializer serializer)
{
writer.WriteValue(Convert.ToBase64String(value.ToArray()));
}
public override TagLib.ByteVector ReadJson(JsonReader reader, Type objectType, TagLib.ByteVector existingValue, bool hasExistingValue, JsonSerializer serializer)
{
return [];
}
}
/// <summary>
/// What should be exported in the output JSON/CSV metadata file.
/// </summary>
public enum MetadataExportType
{
/// <summary>
/// Common files fetched by TagLib
/// </summary>
COMMON = 0,
/// <summary>
/// Everything fetched by TagLib
/// </summary>
ALL_TAGLIB = 1,
/// <summary>
/// Everything fetched by TagLib, but in the container-specific syntax
/// </summary>
ALL_TAGLIB_SPECIFIC_ONLY = 2,
/// <summary>
/// Everything fetched by TagLib, plus the custom metadata array created by TagLibSharp-Web
/// </summary>
ALL_APP = 3
}
}