-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3408a72
commit a397aac
Showing
9 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<topic id="GenerateWithJsonNetAttributes" revisionNumber="1"> | ||
<developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<introduction> | ||
<para>This sample generates a new <codeEntityReference>T:Newtonsoft.Json.Schema.JSchema</codeEntityReference> | ||
from a .NET type with JSchema serialization attributes.</para> | ||
</introduction> | ||
<section> | ||
<title>Sample</title> | ||
<content> | ||
<code lang="cs" source="..\Src\Newtonsoft.Json.Schema.Tests\Documentation\Samples\Generation\GenerateWithJSchemaAttributes.cs" region="Types" title="Types" /> | ||
<code lang="cs" source="..\Src\Newtonsoft.Json.Schema.Tests\Documentation\Samples\Generation\GenerateWithJSchemaAttributes.cs" region="Usage" title="Usage" /> | ||
</content> | ||
</section> | ||
</developerConceptualDocument> | ||
</topic> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...nsoft.Json.Schema.Tests/Documentation/Samples/Generation/GenerateWithJSchemaAttributes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#region License | ||
// Copyright (c) Newtonsoft. All Rights Reserved. | ||
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md | ||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Text; | ||
using Newtonsoft.Json.Schema.Generation; | ||
using Newtonsoft.Json.Serialization; | ||
#if DNXCORE50 | ||
using Xunit; | ||
using Test = Xunit.FactAttribute; | ||
using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert; | ||
#else | ||
using NUnit.Framework; | ||
#endif | ||
|
||
namespace Newtonsoft.Json.Schema.Tests.Documentation.Samples.Generation | ||
{ | ||
[TestFixture] | ||
public class GenerateWithJSchemaAttributes : TestFixtureBase | ||
{ | ||
#region Types | ||
public class Computer | ||
{ | ||
// always require a string value | ||
[UniqueItems] | ||
public IEnumerable<string> DiskIds { get; set; } | ||
|
||
public HashSet<string> ScreenIds { get; set; } | ||
|
||
} | ||
#endregion | ||
|
||
[Test] | ||
public void Example() | ||
{ | ||
#region Usage | ||
JSchemaGenerator generator = new JSchemaGenerator(); | ||
|
||
JSchema schema = generator.Generate(typeof(Computer)); | ||
//{ | ||
// "type": "object", | ||
// "properties": { | ||
// "DiskIds": { | ||
// "type": "array", | ||
// "items": { | ||
// "type": "string", | ||
// }, | ||
// "uniqueItems": true | ||
// }, | ||
// "ScreenIds": { | ||
// "type": "array", | ||
// "items": { | ||
// "type": "string", | ||
// }, | ||
// "uniqueItems": true | ||
// } | ||
// } | ||
//} | ||
#endregion | ||
|
||
Assert.IsTrue(schema.Properties["DiskIds"].UniqueItems); | ||
Assert.IsTrue(schema.Properties["ScreenIds"].UniqueItems); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Src/Newtonsoft.Json.Schema/Generation/UniqueItemsAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace Newtonsoft.Json.Schema.Generation | ||
{ | ||
|
||
/// <summary> | ||
/// Instructs the <see cref="JSchemaGenerator"/> to add unique items is true to the member. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Parameter, AllowMultiple = false)] | ||
public class UniqueItemsAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters