JsonPrimitive.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 » JsonPrimitive.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

namespace System.Json{
  public class JsonPrimitive : JsonValue
  {
    object value;

    public JsonPrimitive (bool value)
    {
      this.value = value;
    }

    public JsonPrimitive (byte value)
    {
      this.value = value;
    }

    public JsonPrimitive (char value)
    {
      this.value = value;
    }

    public JsonPrimitive (decimal value)
    {
      this.value = value;
    }

    public JsonPrimitive (double value)
    {
      this.value = value;
    }

    public JsonPrimitive (float value)
    {
      this.value = value;
    }

    public JsonPrimitive (int value)
    {
      this.value = value;
    }

    public JsonPrimitive (long value)
    {
      this.value = value;
    }

    public JsonPrimitive (sbyte value)
    {
      this.value = value;
    }

    public JsonPrimitive (short value)
    {
      this.value = value;
    }

    public JsonPrimitive (string value)
    {
      this.value = value;
    }

    public JsonPrimitive (DateTime value)
    {
      this.value = value;
    }

    public JsonPrimitive (uint value)
    {
      this.value = value;
    }

    public JsonPrimitive (ulong value)
    {
      this.value = value;
    }

    public JsonPrimitive (ushort value)
    {
      this.value = value;
    }

    public JsonPrimitive (DateTimeOffset value)
    {
      this.value = value;
    }

    public JsonPrimitive (Guid value)
    {
      this.value = value;
    }

    public JsonPrimitive (TimeSpan value)
    {
      this.value = value;
    }

    public JsonPrimitive (Uri value)
    {
      this.value = value;
    }

    internal object Value {
      get { return value; }
    }

    public override JsonType JsonType {
      get {
        // FIXME: what should we do for null? Handle it as null so far.
        if (value == null)
          return JsonType.String;

        switch (Type.GetTypeCode (value.GetType ())) {
        case TypeCode.Boolean:
          return JsonType.Boolean;
        case TypeCode.Char:
        case TypeCode.String:
        case TypeCode.DateTime:
        case TypeCode.Object: // DateTimeOffset || Guid || TimeSpan || Uri
          return JsonType.String;
        default:
          return JsonType.Number;
        }
      }
    }

    static readonly byte [] true_bytes = Encoding.UTF8.GetBytes ("true");
    static readonly byte [] false_bytes = Encoding.UTF8.GetBytes ("false");

    public override void Save (Stream stream)
    {
      switch (JsonType) {
      case JsonType.Boolean:
        if ((bool) value)
          stream.Write (true_bytes, 0, 4);
        else
          stream.Write (false_bytes, 0, 5);
        break;
      case JsonType.String:
        stream.WriteByte ((byte) '\"');
        byte [] bytes = Encoding.UTF8.GetBytes (EscapeString (value.ToString ()));
        stream.Write (bytes, 0, bytes.Length);
        stream.WriteByte ((byte) '\"');
        break;
      default:
        bytes = Encoding.UTF8.GetBytes (GetFormattedString ());
        stream.Write (bytes, 0, bytes.Length);
        break;
      }
    }

    internal string GetFormattedString ()
    {
      switch (JsonType) {
      case JsonType.String:
        if (value is string || value == null)
          return (string) value;
        throw new NotImplementedException ("GetFormattedString from value type " + value.GetType ());
      case JsonType.Number:
        return ((IFormattable) value).ToString ("G", NumberFormatInfo.InvariantInfo);
      default:
        throw new InvalidOperationException ();
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.