forked from idenys/MXTires.Microdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThing.cs
More file actions
211 lines (189 loc) · 10.1 KB
/
Thing.cs
File metadata and controls
211 lines (189 loc) · 10.1 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
#region License
// Copyright (c) 2015 1010Tires.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using MXTires.Microdata.Validators;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using MXTires.Microdata.Intangible;
using MXTires.Microdata.CreativeWorks;
namespace MXTires.Microdata
{
/// <summary>
/// The most generic type of item.
/// </summary>
public class Thing
{
#pragma warning disable 0414
object context = "http://schema.org";
/// <summary>
/// Context
/// </summary>
[JsonProperty("@context", Order = 1)]
public virtual object Context { get { return context; } set { context = value; } }
/// <summary>
/// Property for External Extensions
/// </summary>
internal IList<Property> ExternalExtensions { get; set; }
/// <summary>
/// Type tag
/// </summary>
[JsonProperty("@type", Order = 2)]
public virtual string Type { get { return this.GetType().Name; } }
/// <summary>
/// ID
/// </summary>
[JsonProperty("@id", NullValueHandling = NullValueHandling.Ignore)]
public string Id { get; set; }
/// <summary>
/// URL An additional type for the item, typically used for adding more specific types from external vocabularies in microdata syntax.
/// This is a relationship between something and a class that the thing is in. In RDFa syntax, it is better to use the native RDFa syntax - the
/// 'typeof' attribute - for multiple types. Schema.org tools may have only weaker understanding of extra types, in particular those
/// defined externally.
/// </summary>
[JsonProperty("additionalType")]
public string AdditionalType { get; set; }
/// <summary>
/// Text An alias for the item.
/// </summary>
[JsonProperty("alternateName")]
public string AlternateName { get; set; }
/// <summary>
/// Text A short description of the item.
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }
object image;
/// <summary>
/// URL to an image of the item. This can be a URL or a fully described ImageObject.
/// </summary>
[JsonProperty("image")]
public object Image
{
get { return image; }
set
{
var validator = new TypeValidator(new List<Type>(){typeof(String), typeof(ImageObject), typeof(String[]), typeof(ImageObject[])});
validator.Validate(value);
image = value;
}
}
/// <summary>
/// Text The name of the item.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
/// <summary>
/// Action - Indicates a potential Action, which describes an idealized action in which this thing would play an 'object' role.
/// </summary>
[JsonProperty("potentialAction")]
public Microdata.Action PotentialAction { get; set; }
/// <summary>
/// URL of a reference Web page that unambiguously indicates the item's identity. E.g. the URL of the item's Wikipedia page, Freebase page, or official website.
/// </summary>
[JsonProperty("sameAs")]
public IList<string> SameAs { get; set; }
/// <summary>
/// URL of the item.
/// </summary>
[JsonProperty("url")]
public string Url { get; set; }
#pragma warning restore 0414
private object mainEntityOfPage;
/// <summary>
/// URL or CreativeWork - Indicates a page (or other CreativeWork) for which this thing is the main entity being described.
/// Many (but not all) pages have a fairly clear primary topic, some entity or thing that the page describes. For example a restaurant's home page might be primarily about that Restaurant, or an event listing page might represent a single event. The mainEntity and mainEntityOfPage properties allow you to explicitly express the relationship between the page and the primary entity.
/// Related properties include sameAs, about, and url.
/// The sameAs and url properties are both similar to mainEntityOfPage. The url property should be reserved to refer to more official or authoritative web pages, such as the item’s official website. The sameAs property also relates a thing to a page that indirectly identifies it. Whereas sameAs emphasises well known pages, the mainEntityOfPage property serves more to clarify which of several entities is the main one for that page.
/// mainEntityOfPage can be used for any page, including those not recognized as authoritative for that entity. For example, for a product, sameAs might refer to a page on the manufacturer’s official site with specs for the product, while mainEntityOfPage might be used on pages within various retailers’ sites giving details for the same product.
/// about is similar to mainEntity, with two key differences. First, about can refer to multiple entities/topics, while mainEntity should be used for only the primary one. Second, some pages have a primary entity that itself describes some other entity. For example, one web page may display a news article about a particular person. Another page may display a product review for a particular product. In these cases, mainEntity for the pages should refer to the news article or review, respectively, while about would more properly refer to the person or product.
/// Inverse property: mainEntity.
/// </summary>
[JsonProperty("mainEntityOfPage")]
public object MainEntityOfPage
{
get { return mainEntityOfPage; }
set
{
var validator = new TypeValidator("MXTires.Microdata.CreativeWorks", null, new List<Type>(new Type[] { typeof(String), typeof(WebSite) }));
validator.Validate(value);
mainEntityOfPage = value;
}
}
/// <summary>
/// Returns Json string that represents current object.
/// JSON written by the serializer with an option of Formatting.None and NullValueHandling.Ignore. It makes the JSON result small, skipping all unnecessary
/// spaces and line breaks to produce the most compact and efficient JSON possible.
/// </summary>
/// <returns></returns>
public string ToJson()
{
string item = JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
item = item.Replace("Context", "@context");
if (item.Equals("Type")) item = item.Replace("Type", "@type");
return "<script type=\"application/ld+json\">" + item + "</script>";
}
/// <summary>
/// Returns Json string that represents current object.
/// JSON written by the serializer with an option of Formatting.Indented produces nicely formatted, easy to read JSON – great when you are developing.
/// </summary>
/// <returns></returns>
public string ToIndentedJson()
{
string item = JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
item = item.Replace("Context", "@context");
if (item.Equals("Type")) item = item.Replace("Type", "@type");
return "<script type=\"application/ld+json\">" + item + "</script>";
}
/// <summary>
/// Returns Json string that represents current object.
/// JSON written by the serializer with an option of Formatting.None and NullValueHandling.Ignore. It makes the JSON result small, skipping all unnecessary
/// spaces and line breaks to produce the most compact and efficient JSON possible.
/// </summary>
/// <returns></returns>
public override string ToString()
{
var item = JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
item = item.Replace("Context", "@context");
if (item.Equals("Type")) item = item.Replace("Type", "@type");
return item;
}
//#region Delegate and Events
///// <summary>
///// OnValidateEventHandler delegate to enable injection of custom validation routines
///// </summary>
//public delegate void OnValidateEventHandler(object sender, EventArgs e);
//public delegate void OnValidatedEventHandler(object sender, EventArgs e);
//public OnValidateEventHandler OnValidate;
//public OnValidatedEventHandler OnValidated;
//#endregion
//#region Internal Fields
///// <summary>
///// The Errors collection to keep the errors. Tthe validation method populates this.
///// </summary>
//public List<error> Errors = new List<error>();
//#endregion
}
}