Files
libremetaverse/libsecondlife-cs/JSONlib/JSONUtils.cs
John Hurliman b4accdddfc * Added JSON library to parse LLSD fields in the login reply
* Added fields in Avatar to hold the parsed data from login
* CoarseLocationUpdate packets handled internally
* Added Network.LoginValues hashtable, removed second parameter from .Login()
* Updated examples to reflect LoginValues / Login() change

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@39 52acb1d6-8a22-11de-b505-999d5b087335
2006-07-01 03:42:53 +00:00

70 lines
1.6 KiB
C#

using System;
using System.Text;
namespace Nii.JSON
{
/// <summary>
/// Public Domain 2002 JSON.org
/// @author JSON.org
/// @version 0.1
/// Ported to C# by Are Bjolseth, teleplan.no
/// </summary>
public sealed class JSONUtils
{
/// <summary>
/// Produce a string in double quotes with backslash sequences in all the right places.
/// </summary>
/// <param name="s">A String</param>
/// <returns>A String correctly formatted for insertion in a JSON message.</returns>
public static string Enquote(string s)
{
if (s == null || s.Length == 0)
{
return "\"\"";
}
char c;
int i;
int len = s.Length;
StringBuilder sb = new StringBuilder(len + 4);
string t;
sb.Append('"');
for (i = 0; i < len; i += 1)
{
c = s[i];
if ((c == '\\') || (c == '"') || (c == '>'))
{
sb.Append('\\');
sb.Append(c);
}
else if (c == '\b')
sb.Append("\\b");
else if (c == '\t')
sb.Append("\\t");
else if (c == '\n')
sb.Append("\\n");
else if (c == '\f')
sb.Append("\\f");
else if (c == '\r')
sb.Append("\\r");
else
{
if (c < ' ')
{
//t = "000" + Integer.toHexString(c);
string tmp = new string(c,1);
t = "000" + int.Parse(tmp,System.Globalization.NumberStyles.HexNumber);
sb.Append("\\u" + t.Substring(t.Length - 4));
}
else
{
sb.Append(c);
}
}
}
sb.Append('"');
return sb.ToString();
}
}
}