internal unsafe static string ToStringIfString(object value, JsonSettings settings) { var str = value as string; if (str != null) { StringBuilder sb = new StringBuilder(); Json.EncodedJSONString(sb, str, settings); return(sb.ToString()); } return(str); }
internal unsafe static object ToStringIfStringObject(object value, JsonSettings settings) { var str = value as string; if (str != null) { str = string.Concat('"', str, '"'); fixed(char *p = str) { char *ptr = p; int index = 0; return(Json.DecodeJSONString(ptr, ref index, settings, fromObject: true)); } } return(value); }
internal static bool IsCurrentAQuot(char current, JsonSettings settings) { if (settings.HasOverrideQuoteChar) { return(current == settings.QuoteChar); } var quote = settings.QuoteChar; var isQuote = current == QuotSingleChar || current == QuotDoubleChar; if (isQuote) { if (quote != current) { settings.QuoteCharString = (settings.QuoteChar = current).ToString(); } settings.HasOverrideQuoteChar = true; } return(isQuote); }
internal static string FlagEnumToString(object value, JsonSettings settings) { if (settings.UseEnumString) { return(((Enum)value).ToString()); } var eType = value.GetType().GetTypeInfo().GetEnumUnderlyingType(); if (eType == Json._intType) { return(IntToStr((int)value)); } else if (eType == Json._longType) { return(LongToStr((long)value)); } else if (eType == typeof(ulong)) { return(LongToStr((long)((ulong)value))); } else if (eType == typeof(uint)) { return(QuickConvert.C2Str((uint)value)); } else if (eType == typeof(byte)) { return(IntToStr((int)((byte)value))); } else if (eType == typeof(ushort)) { return(IntToStr((int)((ushort)value))); } else if (eType == typeof(short)) { return(IntToStr((int)((short)value))); } return(IntToStr((int)value)); }
public abstract T Deserialize(TextReader reader, JsonSettings settings);
public abstract void Serialize(T value, TextWriter writer, JsonSettings settings);
public abstract T Deserialize(string value, JsonSettings settings);
//With Settings public abstract string Serialize(T value, JsonSettings settings);
internal static unsafe void SkipProperty(char *ptr, ref int index, JsonSettings settings) { var currentIndex = index; char current = '\0'; char bchar = '\0'; char echar = '\0'; bool isStringType = false; bool isNonStringType = false; int counter = 0; bool hasChar = false; var currentQuote = settings.QuoteChar; while (true) { current = *(ptr + index); if (current != ' ' && current != ':' && current != '\n' && current != '\r' && current != '\t') { if (!hasChar) { isStringType = current == currentQuote; if (!isStringType) { isNonStringType = current != '{' && current != '['; } if (isStringType || isNonStringType) { break; } bchar = current; echar = current == '{' ? '}' : current == '[' ? ']' : '\0'; counter = 1; hasChar = true; } else { if ((current == '{' && bchar == '{') || (current == '[' && bchar == '[')) { counter++; } else if ((current == '}' && echar == '}') || (current == ']' && echar == ']')) { counter--; } } } index++; if (hasChar && counter == 0) { break; } } if (isStringType || isNonStringType) { index = currentIndex; if (isStringType) { GetStringBasedValue(ptr, ref index, settings); } else if (isNonStringType) { GetNonStringValue(ptr, ref index); } } }
private static DateTime StringToDate(string value, JsonSettings settings, out TimeSpan offset, bool isDateTimeOffset) { offset = TimeSpan.Zero; if (settings.HasDateStringFormat) { return(DateTime.ParseExact(value, settings.DateStringFormat, CultureInfo.CurrentCulture)); } if (settings.DateFormat == JsonDateFormat.EpochTime) { var unixTimeStamp = FastStringToLong(value); var date = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); return(date.AddTicks(unixTimeStamp).ToLocalTime()); } DateTime dt; string[] tokens = null; bool negative = false; string offsetText = null; int tickMilliseconds = 0; bool noOffSetValue = false; var timeZoneFormat = settings.TimeZoneFormat; if (value == "\\/Date(-62135596800)\\/") { return(DateTime.MinValue); } else if (value == "\\/Date(253402300800)\\/") { return(DateTime.MaxValue); } else if (value[0] == '\\') { var dateText = value.Substring(7, value.IndexOf(')', 7) - 7); negative = dateText.IndexOf('-') >= 0; tokens = negative ? dateText.Split(_dateNegChars, StringSplitOptions.RemoveEmptyEntries) : dateText.Split(_datePosChars, StringSplitOptions.RemoveEmptyEntries); dateText = tokens[0]; var ticks = FastStringToLong(dateText); var multiply = settings.DateFormat == JsonDateFormat.JavascriptSerializer ? TimeSpan.TicksPerMillisecond : 1; dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dt = dt.AddTicks(ticks * multiply); if (timeZoneFormat == JsonTimeZoneFormat.Unspecified || timeZoneFormat == JsonTimeZoneFormat.Utc) { dt = dt.ToLocalTime(); } var kind = timeZoneFormat == JsonTimeZoneFormat.Local ? DateTimeKind.Local : timeZoneFormat == JsonTimeZoneFormat.Utc ? DateTimeKind.Utc : DateTimeKind.Unspecified; dt = new DateTime(dt.Ticks, kind); offsetText = tokens.Length > 1 ? tokens[1] : offsetText; } else { var dateText = value.Substring(0, 19); var diff = value.Length - dateText.Length; var hasOffset = diff > 0; var utcOffsetText = hasOffset ? value.Substring(dateText.Length, diff) : string.Empty; var firstChar = utcOffsetText.Length > 0 ? utcOffsetText[0] : '\0'; negative = diff > 0 && firstChar == '-'; if (hasOffset) { noOffSetValue = timeZoneFormat == JsonTimeZoneFormat.Utc || timeZoneFormat == JsonTimeZoneFormat.Unspecified; offsetText = utcOffsetText.Substring(1, utcOffsetText.Length - 1).Replace(":", string.Empty).Replace("Z", string.Empty); if (timeZoneFormat == JsonTimeZoneFormat.Local) { int indexOfSign = offsetText.IndexOf('-'); negative = indexOfSign >= 0; if (!negative) { indexOfSign = offsetText.IndexOf('+'); } tickMilliseconds = FastStringToInt(offsetText.Substring(0, indexOfSign)); offsetText = offsetText.Substring(indexOfSign + 1, offsetText.Length - indexOfSign - 1); if (negative) { offsetText = offsetText.Replace("-", string.Empty); } else { offsetText = offsetText.Replace("+", string.Empty); } } else { tickMilliseconds = FastStringToInt(offsetText); } } dt = DateTime.Parse(dateText, CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal); if (timeZoneFormat == JsonTimeZoneFormat.Local) { if (!isDateTimeOffset) { dt = dt.ToUniversalTime(); } dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, DateTimeKind.Local); } else if (timeZoneFormat == JsonTimeZoneFormat.Utc) { dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond, DateTimeKind.Utc); } } var isNullOrWhiteSpace = string.IsNullOrWhiteSpace(offsetText); if (!isNullOrWhiteSpace) { var hours = noOffSetValue ? 0 : FastStringToInt(offsetText.Substring(0, 2)); var minutes = noOffSetValue ? 0 : (offsetText.Length > 2 ? FastStringToInt(offsetText.Substring(2, 2)) : 0); if (negative) { hours *= -1; } offset = new TimeSpan(hours, minutes, 0); if (!isDateTimeOffset) { dt = dt.AddHours(hours).AddMinutes(minutes); } dt = dt.AddTicks(tickMilliseconds); } return(dt); }
internal static bool NeedQuotes(Type type, JsonSettings settings) { return(Json.NeedQuotes(type, settings)); }
internal static DateTime FastStringToDate(string value, JsonSettings settings) { TimeSpan offset; return(StringToDate(value, settings, out offset, isDateTimeOffset: false)); }
internal unsafe static bool IsInRange(char *ptr, ref int index, int offset, string key, JsonSettings settings) { var inRangeChr = *(ptr + index + offset + 2); fixed(char *kPtr = key) { return((*(ptr + index) == settings.QuoteChar && (inRangeChr == ':' || inRangeChr == ' ' || inRangeChr == '\t' || inRangeChr == '\n' || inRangeChr == '\r')) && *(ptr + index + 1) == *kPtr); } }
internal static string AllDateOffsetToString(DateTimeOffset offset, JsonSettings settings) { return(DateToStringWithOffset(offset.DateTime, settings, offset.Offset)); }
internal static string AllDateToString(DateTime date, JsonSettings settings) { var offset = TimeZoneInfo.Local.GetUtcOffset(date); return(DateToStringWithOffset(date, settings, offset)); }