~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

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;
27
 
using System.Collections.Generic;
28
 
using System.Linq;
29
 
using Newtonsoft.Json.Linq;
30
 
using Newtonsoft.Json.Utilities;
31
 
 
32
 
namespace Newtonsoft.Json.Schema
33
 
{
34
 
  internal class JsonSchemaModel
35
 
  {
36
 
    public bool Optional { get; set; }
37
 
    public JsonSchemaType Type { get; set; }
38
 
    public int? MinimumLength { get; set; }
39
 
    public int? MaximumLength { get; set; }
40
 
    public int? MaximumDecimals { get; set; }
41
 
    public double? Minimum { get; set; }
42
 
    public double? Maximum { 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 JsonSchemaModel AdditionalProperties { get; set; }
49
 
    public bool AllowAdditionalProperties { get; set; }
50
 
    public IList<JToken> Enum { get; set; }
51
 
    public JsonSchemaType Disallow { get; set; }
52
 
 
53
 
    public JsonSchemaModel()
54
 
    {
55
 
      Type = JsonSchemaType.Any;
56
 
      AllowAdditionalProperties = true;
57
 
      Optional = true;
58
 
    }
59
 
 
60
 
    public static JsonSchemaModel Create(IList<JsonSchema> schemata)
61
 
    {
62
 
      JsonSchemaModel model = new JsonSchemaModel();
63
 
 
64
 
      foreach (JsonSchema schema in schemata)
65
 
      {
66
 
        Combine(model, schema);
67
 
      }
68
 
 
69
 
      return model;
70
 
    }
71
 
 
72
 
    private static void Combine(JsonSchemaModel model, JsonSchema schema)
73
 
    {
74
 
      model.Optional = model.Optional && (schema.Optional ?? false);
75
 
      model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
76
 
 
77
 
      model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
78
 
      model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
79
 
      model.MaximumDecimals = MathUtils.Min(model.MaximumDecimals, schema.MaximumDecimals);
80
 
      model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
81
 
      model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
82
 
      model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
83
 
      model.MaximumItems = MathUtils.Min(model.MaximumItems, schema.MaximumItems);
84
 
      model.AllowAdditionalProperties = model.AllowAdditionalProperties && schema.AllowAdditionalProperties;
85
 
      if (schema.Enum != null)
86
 
      {
87
 
        if (model.Enum == null)
88
 
          model.Enum = new List<JToken>();
89
 
 
90
 
        model.Enum.AddRangeDistinct(schema.Enum, new JTokenEqualityComparer());
91
 
      }
92
 
      model.Disallow = model.Disallow | (schema.Disallow ?? JsonSchemaType.None);
93
 
 
94
 
      if (schema.Pattern != null)
95
 
      {
96
 
        if (model.Patterns == null)
97
 
          model.Patterns = new List<string>();
98
 
 
99
 
        model.Patterns.AddDistinct(schema.Pattern);
100
 
      }
101
 
    }
102
 
  }
 
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
  }
103
111
}
 
 
b'\\ No newline at end of file'