forked from arunmariappan/Ext.NET.Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonUtils.cs
More file actions
75 lines (71 loc) · 2.4 KB
/
Copy pathJsonUtils.cs
File metadata and controls
75 lines (71 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/********
* @version : 1.3.0
* @author : Ext.NET, Inc. http://www.ext.net/
* @date : 2012-02-29
* @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license : See license.txt and http://www.ext.net/license/.
********/
using System.Text;
namespace Ext.Net.Utilities
{
public class JsonUtils
{
// Public Domain 2002 JSON.org
// @author JSON.org
// @version 0.1
// Ported to C# by Are Bjolseth, teleplan.no
/// <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();
}
}
}