~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Schema/JsonSchemaModel.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System.Collections.Generic;
 
27
using Newtonsoft.Json.Linq;
 
28
using Newtonsoft.Json.Utilities;
 
29
 
 
30
namespace Newtonsoft.Json.Schema
 
31
{
 
32
  internal class JsonSchemaModel
 
33
  {
 
34
    public bool Required { get; set; }
 
35
    public JsonSchemaType Type { get; set; }
 
36
    public int? MinimumLength { get; set; }
 
37
    public int? MaximumLength { get; set; }
 
38
    public double? DivisibleBy { get; set; }
 
39
    public double? Minimum { get; set; }
 
40
    public double? Maximum { get; set; }
 
41
    public bool ExclusiveMinimum { get; set; }
 
42
    public bool ExclusiveMaximum { get; set; }
 
43
    public int? MinimumItems { get; set; }
 
44
    public int? MaximumItems { get; set; }
 
45
    public IList<string> Patterns { get; set; }
 
46
    public IList<JsonSchemaModel> Items { get; set; }
 
47
    public IDictionary<string, JsonSchemaModel> Properties { get; set; }
 
48
    public IDictionary<string, JsonSchemaModel> PatternProperties { get; set; }
 
49
    public JsonSchemaModel AdditionalProperties { get; set; }
 
50
    public bool AllowAdditionalProperties { get; set; }
 
51
    public IList<JToken> Enum { get; set; }
 
52
    public JsonSchemaType Disallow { get; set; }
 
53
 
 
54
    public JsonSchemaModel()
 
55
    {
 
56
      Type = JsonSchemaType.Any;
 
57
      AllowAdditionalProperties = true;
 
58
      Required = false;
 
59
    }
 
60
 
 
61
    public static JsonSchemaModel Create(IList<JsonSchema> schemata)
 
62
    {
 
63
      JsonSchemaModel model = new JsonSchemaModel();
 
64
 
 
65
      foreach (JsonSchema schema in schemata)
 
66
      {
 
67
        Combine(model, schema);
 
68
      }
 
69
 
 
70
      return model;
 
71
    }
 
72
 
 
73
    private static void Combine(JsonSchemaModel model, JsonSchema schema)
 
74
    {
 
75
      // Version 3 of the Draft JSON Schema has the default value of Not Required
 
76
      model.Required = model.Required || (schema.Required ?? false);
 
77
      model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
 
78
 
 
79
      model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
 
80
      model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
 
81
 
 
82
      // not sure what is the best way to combine divisibleBy
 
83
      model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
 
84
 
 
85
      model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
 
86
      model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
 
87
      model.ExclusiveMinimum = model.ExclusiveMinimum || (schema.ExclusiveMinimum ?? false);
 
88
      model.ExclusiveMaximum = model.ExclusiveMaximum || (schema.ExclusiveMaximum ?? false);
 
89
 
 
90
      model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
 
91
      model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
 
92
      model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
 
93
      if (schema.Enum != null)
 
94
      {
 
95
        if (model.Enum == null)
 
96
          model.Enum = new List<JToken>();
 
97
 
 
98
        model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
 
99
      }
 
100
      model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
 
101
 
 
102
      if (schema.Pattern != null)
 
103
      {
 
104
        if (model.Patterns == null)
 
105
          model.Patterns = new List<string>();
 
106
 
 
107
        model.Patterns.AddDistinct(schema.Pattern);
 
108
      }
 
109
    }
 
110
  }
 
111
}
 
 
b'\\ No newline at end of file'