-
Notifications
You must be signed in to change notification settings - Fork 0
/
LayoutDataResource.cs
68 lines (61 loc) · 2.63 KB
/
LayoutDataResource.cs
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
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Text;
namespace DynamicPDF.Api
{
/// <summary>
/// Represents the Layout data resource used to create PDF reports.
/// </summary>
public class LayoutDataResource : Resource
{
/// <summary>
/// Initializes a new instance of the <see cref="LayoutDataResource"/> class
/// using the layout data object and a resource name.
/// </summary>
/// <param name="layoutData">Serializable object data to create PDF report.</param>
/// <param name="layoutDataResourceName">The name for layout data resource.</param>
public LayoutDataResource(Object layoutData, string layoutDataResourceName = null) : base()
{
String jsonText = JsonConvert.SerializeObject(layoutData, new JsonSerializerSettings
{
});
Data = Encoding.UTF8.GetBytes(jsonText);
if (LayoutDataResourceName == null)
LayoutDataResourceName = Guid.NewGuid().ToString() + ".json";
else
LayoutDataResourceName = layoutDataResourceName;
}
/// <summary>
/// Initializes a new instance of the <see cref="LayoutDataResource"/> class
/// using a JSON file with data and a resource name.
/// </summary>
/// <param name="layoutData">The layout data JSON file path or JSON string.</param>
/// <param name="layoutDataResourceName">The name for layout data resource.</param>
public LayoutDataResource(string layoutData, string layoutDataResourceName = null) : base()
{
if (layoutData.EndsWith(".json"))
{
Data = Resource.GetUTF8FileData(layoutData);
}
else
{
Data = Encoding.UTF8.GetBytes(layoutData);
}
if (LayoutDataResourceName == null)
LayoutDataResourceName = Guid.NewGuid().ToString() + ".json";
else
LayoutDataResourceName = layoutDataResourceName;
}
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter), converterParameters: typeof(CamelCaseNamingStrategy))]
internal override ResourceType Type { get; } = ResourceType.LayoutData;
internal override string FileExtension { get; } = ".json";
internal override string MimeType { get; set; } = "application/json";
/// <summary>
/// Gets or sets name of the layout data resource.
/// </summary>
public string LayoutDataResourceName { get; set; }
}
}