/* Copyright 2010-2016 MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Globalization;
namespace MongoDB.Bson.IO
{
///
/// Encodes and decodes scalar values to JSON compatible strings.
///
public static class JsonConvert
{
///
/// Converts a string to a Boolean.
///
/// The value.
/// A Boolean.
public static bool ToBoolean(string value)
{
return bool.Parse(value);
}
///
/// Converts a string to a DateTime.
///
/// The value.
/// A DateTime.
public static DateTime ToDateTime(string value)
{
var formats = new[]
{
"yyyy-MM-ddK",
"yyyy-MM-ddTHH:mm:ssK",
"yyyy-MM-ddTHH:mm:ss.FFFFFFFK"
};
var style = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal;
return DateTime.ParseExact(value, formats, DateTimeFormatInfo.InvariantInfo, style);
}
///
/// Converts a string to a DateTimeOffset.
///
/// The value.
/// A DateTimeOffset.
public static DateTimeOffset ToDateTimeOffset(string value)
{
return DateTimeOffset.ParseExact(value, "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", DateTimeFormatInfo.InvariantInfo);
}
///
/// Converts a string to a Decimal.
///
/// The value.
/// A Decimal.
public static decimal ToDecimal(string value)
{
return decimal.Parse(value, NumberFormatInfo.InvariantInfo);
}
///
/// Converts a string to a .
///
/// The value.
/// A .
public static Decimal128 ToDecimal128(string value)
{
return Decimal128.Parse(value);
}
///
/// Converts a string to a Double.
///
/// The value.
/// A Double.
public static double ToDouble(string value)
{
return double.Parse(value, NumberFormatInfo.InvariantInfo);
}
///
/// Converts a string to an Int16.
///
/// The value.
/// An Int16.
public static short ToInt16(string value)
{
return Int16.Parse(value);
}
///
/// Converts a string to an Int32.
///
/// The value.
/// An Int32.
public static int ToInt32(string value)
{
return Int32.Parse(value);
}
///
/// Converts a string to an Int64.
///
/// The value.
/// An Int64.
public static long ToInt64(string value)
{
return Int64.Parse(value);
}
///
/// Converts a string to a Single.
///
/// The value.
/// A Single.
public static float ToSingle(string value)
{
return float.Parse(value, NumberFormatInfo.InvariantInfo);
}
///
/// Converts a Boolean to a string.
///
/// The value.
/// A string.
public static string ToString(bool value)
{
return value ? "true" : "false";
}
///
/// Converts a DateTime to a string.
///
/// The value.
/// A string.
public static string ToString(DateTime value)
{
return value.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK", DateTimeFormatInfo.InvariantInfo);
}
///
/// Converts a DateTimeOffset to a string.
///
/// The value.
/// A string.
public static string ToString(DateTimeOffset value)
{
return value.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK", DateTimeFormatInfo.InvariantInfo);
}
///
/// Converts a Decimal to a string.
///
/// The value.
/// A string.
public static string ToString(decimal value)
{
return value.ToString("G", NumberFormatInfo.InvariantInfo);
}
///
/// Converts a to a string.
///
/// The value.
/// A string.
public static string ToString(Decimal128 value)
{
return value.ToString();
}
///
/// Converts a Double to a string.
///
/// The value.
/// A string.
public static string ToString(double value)
{
return value.ToString("R", NumberFormatInfo.InvariantInfo);
}
///
/// Converts a Single to a string.
///
/// The value.
/// A string.
public static string ToString(float value)
{
return value.ToString("R", NumberFormatInfo.InvariantInfo);
}
///
/// Converts an Int32 to a string.
///
/// The value.
/// A string.
public static string ToString(int value)
{
return value.ToString();
}
///
/// Converts an Int64 to a string.
///
/// The value.
/// A string.
public static string ToString(long value)
{
return value.ToString();
}
///
/// Converts an Int16 to a string.
///
/// The value.
/// A string.
public static string ToString(short value)
{
return value.ToString();
}
///
/// Converts a UInt32 to a string.
///
/// The value.
/// A string.
[CLSCompliant(false)]
public static string ToString(uint value)
{
return value.ToString();
}
///
/// Converts a UInt64 to a string.
///
/// The value.
/// A string.
[CLSCompliant(false)]
public static string ToString(ulong value)
{
return value.ToString();
}
///
/// Converts a UInt16 to a string.
///
/// The value.
/// A string.
[CLSCompliant(false)]
public static string ToString(ushort value)
{
return value.ToString();
}
///
/// Converts a string to a UInt16.
///
/// The value.
/// A UInt16.
[CLSCompliant(false)]
public static ushort ToUInt16(string value)
{
return UInt16.Parse(value);
}
///
/// Converts a string to a UInt32.
///
/// The value.
/// A UInt32.
[CLSCompliant(false)]
public static uint ToUInt32(string value)
{
return UInt32.Parse(value);
}
///
/// Converts a string to a UInt64.
///
/// The value.
/// A UInt64.
[CLSCompliant(false)]
public static ulong ToUInt64(string value)
{
return UInt64.Parse(value);
}
}
}