Determines whether the specified value is json. Deals with null strings - CSharp System.Runtime.Serialization.Json

CSharp examples for System.Runtime.Serialization.Json:Json

Description

Determines whether the specified value is json. Deals with null strings

Demo Code


using System.Text.RegularExpressions;
using System;/*  w ww .ja va  2s .c  om*/

public class Main{
        /// <summary>
        /// Determines whether the specified value is json.
        /// Deals with null strings
        /// </summary>
        /// <param name="source">The value.</param>
        /// <returns>
        ///   <c>true</c> if the specified value is json; otherwise, <c>false</c>.
        /// </returns>
        public static bool SafeIsJson(this string source)
        {
            if (string.IsNullOrEmpty(source))
                return false;

            source = source.Trim();

            return source.StartsWith("{") && source.EndsWith("}")
                || source.StartsWith("[") && source.EndsWith("]");
        }
        /// <summary>
        /// Determines whether the specified source string is null or empty.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>
        ///   <c>true</c> if the specified source string is null or empty; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNullOrEmpty(this string source)
        {
            return string.IsNullOrEmpty(source);
        }
}

Related Tutorials