JsonObject.cs :  » 2.6.4-mono-.net-core » System.Json » System » Json » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.Json 
System.Json » System » Json » JsonObject.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

using JsonPairSystem.Collections.Generic.KeyValuePairSystem.Json.JsonValue;
using JsonPairEnumerableSystem.Collections.Generic.IEnumerableSystem.Collections.Generic.KeyValuePairSystem.Json.JsonValue;


namespace System.Json{
  public class JsonObject : JsonValue, IDictionary<string, JsonValue>, ICollection<KeyValuePair<string, JsonValue>>
  {
    int known_count = -1;
    Dictionary<string, JsonValue> map;
    JsonPairEnumerable source;

    public JsonObject (params KeyValuePair<string, JsonValue> [] items)
      : this ((JsonPairEnumerable) items)
    {
    }

    public JsonObject (JsonPairEnumerable items)
    {
      if (items == null)
        throw new ArgumentNullException ("items");
      this.source = items;
    }

    public override int Count {
      get {
        if (known_count < 0) {
          if (map != null)
            known_count = map.Count;
          else {
            known_count = 0;
            foreach (JsonPair p in source)
              known_count++;
          }
        }
        return known_count;
      }
    }

    public IEnumerator<JsonPair> GetEnumerator ()
    {
      return AsEnumerable ().GetEnumerator ();
    }

    IEnumerator IEnumerable.GetEnumerator ()
    {
      return AsEnumerable ().GetEnumerator ();
    }
      
    JsonPairEnumerable AsEnumerable ()
    {
      return map ?? source;
    }

    public bool IsReadOnly {
      get { return false; }
    }

    bool ICollection<JsonPair>.IsReadOnly {
      get { return ((ICollection<JsonPair>) map).IsReadOnly; }
    }

    public override sealed JsonValue this [string key] {
      get {
        foreach (JsonPair pair in AsEnumerable ())
          if (pair.Key == key)
            return pair.Value;
        throw new KeyNotFoundException (String.Format ("key '{0}' was not found.", key));
      }
      set {
        PopulateMap ();
        map [key] = value;
      }
    }

    public override JsonType JsonType {
      get { return JsonType.Object; }
    }

    public ICollection<string> Keys {
      get {
        PopulateMap ();
        return map.Keys;
      }
    }

    public ICollection<JsonValue> Values {
      get {
        PopulateMap ();
        return map.Values;
      }
    }

    public void Add (string key, JsonValue value)
    {
      if (key == null)
        throw new ArgumentNullException ("key");
      if (value == null)
        throw new ArgumentNullException ("value");
      PopulateMap ();
      map [key] = value;
    }

    public void Add (JsonPair pair)
    {
      Add (pair.Key, pair.Value);
    }

    public void AddRange (JsonPairEnumerable items)
    {
      if (items == null)
        throw new ArgumentNullException ("items");
      source = new MergedEnumerable<JsonPair> (source, items);
      map = null;
    }

    public void AddRange (JsonPair [] items)
    {
      AddRange ((JsonPairEnumerable) items);
    }

    static readonly JsonPair [] empty = new JsonPair [0];

    public void Clear ()
    {
      if (map != null)
        map.Clear ();
      else
        source = empty;
    }

    bool ICollection<JsonPair>.Contains (JsonPair item)
    {
      return ContainsKey (item.Key);
    }

    public override bool ContainsKey (string key)
    {
      if (key == null)
        throw new ArgumentNullException ("key");
      foreach (JsonPair p in AsEnumerable ())
        if (p.Key == key)
          return true;
      return false;
    }

    public void CopyTo (JsonPair [] array, int arrayIndex)
    {
      foreach (JsonPair p in AsEnumerable ())
        array [arrayIndex++] = p;
    }

    public bool Remove (string key)
    {
      PopulateMap ();
      return map.Remove (key);
    }

    bool ICollection<JsonPair>.Remove (JsonPair item)
    {
      return ((ICollection<JsonPair>) map).Remove (item);
    }

    public override void Save (Stream stream)
    {
      if (stream == null)
        throw new ArgumentNullException ("stream");
      stream.WriteByte ((byte) '{');
      foreach (JsonPair pair in this) {
        stream.WriteByte ((byte) '"');
        byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (pair.Key));
        stream.Write (bytes, 0, bytes.Length);
        stream.WriteByte ((byte) '"');
        stream.WriteByte ((byte) ',');
        stream.WriteByte ((byte) ' ');
        pair.Value.Save (stream);
      }
      stream.WriteByte ((byte) '}');
    }

    public bool TryGetValue (string key, out JsonValue value)
    {
      foreach (JsonPair p in AsEnumerable ())
        if (p.Key == key) {
          value = p.Value;
          return true;
        }
      value = null;
      return false;
    }

    void PopulateMap ()
    {
      if (map == null) {
        map = new Dictionary<string, JsonValue> ();
        foreach (JsonPair pair in source)
          map.Add (pair.Key, pair.Value);
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.