本文整理汇总了C#中LitJson.JsonWriter.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# JsonWriter.ToString方法的具体用法?C# JsonWriter.ToString怎么用?C# JsonWriter.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LitJson.JsonWriter
的用法示例。
在下文中一共展示了JsonWriter.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encode
public string Encode(object obj)
{
JsonWriter writer = new JsonWriter();
JsonMapper.ToJson(obj, writer);
return writer.ToString();
}
示例2: SaveTasks
public void SaveTasks(TaskList.SerializedForm serializedForm) {
JsonWriter writer = new JsonWriter();
writer.PrettyPrint = true;
JsonMapper.ToJson(serializedForm, writer);
string json = writer.ToString();
StreamWriter sr = new StreamWriter(this.taskFile);
sr.Write(json);
sr.Close();
}
示例3: SaveSettings
public void SaveSettings(Settings.SerializedForm serializedForm) {
JsonWriter writer = new JsonWriter();
writer.PrettyPrint = true;
JsonMapper.ToJson(serializedForm, writer);
string json = writer.ToString();
StreamWriter sr = new StreamWriter(this.configFile);
sr.Write(json);
sr.Close();
}
示例4: PrettyPrintTest
public void PrettyPrintTest()
{
JsonWriter writer = new JsonWriter ();
string json = @"
[
{
""precision"" : ""zip"",
""Latitude"" : 37.7668,
""Longitude"" : -122.3959,
""City"" : ""SAN FRANCISCO""
},
{
""precision"" : ""zip"",
""Latitude"" : 37.371991,
""Longitude"" : -122.02602,
""City"" : ""SUNNYVALE""
}
]";
writer.PrettyPrint = true;
writer.WriteArrayStart ();
writer.WriteObjectStart ();
writer.WritePropertyName ("precision");
writer.Write ("zip");
writer.WritePropertyName ("Latitude");
writer.Write (37.7668);
writer.WritePropertyName ("Longitude");
writer.Write (-122.3959);
writer.WritePropertyName ("City");
writer.Write ("SAN FRANCISCO");
writer.WriteObjectEnd ();
writer.IndentValue = 2;
writer.WriteObjectStart ();
writer.WritePropertyName ("precision");
writer.Write ("zip");
writer.WritePropertyName ("Latitude");
writer.Write (37.371991);
writer.WritePropertyName ("Longitude");
writer.Write (-122.02602);
writer.WritePropertyName ("City");
writer.Write ("SUNNYVALE");
writer.WriteObjectEnd ();
writer.WriteArrayEnd ();
Assert.AreEqual (writer.ToString (), json);
}
示例5: ObjectTest
public void ObjectTest()
{
JsonWriter writer = new JsonWriter ();
string json = "{\"flavour\":\"strawberry\",\"color\":\"red\"," +
"\"amount\":3}";
writer.WriteObjectStart ();
writer.WritePropertyName ("flavour");
writer.Write ("strawberry");
writer.WritePropertyName ("color");
writer.Write ("red");
writer.WritePropertyName ("amount");
writer.Write (3);
writer.WriteObjectEnd ();
Assert.AreEqual (writer.ToString (), json);
}
示例6: NestedObjectsTest
public void NestedObjectsTest()
{
JsonWriter writer = new JsonWriter ();
string json = "{\"book\":{\"title\":" +
"\"Structure and Interpretation of Computer Programs\"," +
"\"details\":{\"pages\":657}}}";
writer.WriteObjectStart ();
writer.WritePropertyName ("book");
writer.WriteObjectStart ();
writer.WritePropertyName ("title");
writer.Write (
"Structure and Interpretation of Computer Programs");
writer.WritePropertyName ("details");
writer.WriteObjectStart ();
writer.WritePropertyName ("pages");
writer.Write (657);
writer.WriteObjectEnd ();
writer.WriteObjectEnd ();
writer.WriteObjectEnd ();
Assert.AreEqual (writer.ToString (), json);
}
示例7: NestedArraysTest
public void NestedArraysTest()
{
JsonWriter writer = new JsonWriter ();
string json = "[1,[\"a\",\"b\",\"c\"],2,[[null]],3]";
writer.WriteArrayStart ();
writer.Write (1);
writer.WriteArrayStart ();
writer.Write ("a");
writer.Write ("b");
writer.Write ("c");
writer.WriteArrayEnd ();
writer.Write (2);
writer.WriteArrayStart ();
writer.WriteArrayStart ();
writer.Write (null);
writer.WriteArrayEnd ();
writer.WriteArrayEnd ();
writer.Write (3);
writer.WriteArrayEnd ();
Assert.AreEqual (writer.ToString (), json);
}
示例8: JMap
public static string JMap(Map m)
{
LitJson.JsonWriter js = new LitJson.JsonWriter();
js.PrettyPrint = true;
js.IndentValue = 2;
js.WriteObjectStart();
js.WritePropertyName("min");
js.WriteObjectStart();
js.WritePropertyName("x");
js.Write(m.min.x);
js.WritePropertyName("y");
js.Write(m.min.y);
js.WriteObjectEnd();
js.WritePropertyName("max");
js.WriteObjectStart();
js.WritePropertyName("x");
js.Write(m.max.x);
js.WritePropertyName("y");
js.Write(m.max.y);
js.WriteObjectEnd();
js.WritePropertyName("tiles");
js.WriteArrayStart();
List<Map.Tile> tiles = m.GetTiles();
foreach(Map.Tile t in tiles) {
js.WriteObjectStart();
// Key
js.WritePropertyName("key");
js.WriteObjectStart();
js.WritePropertyName("x");
js.Write(t.hexCoord.x);
js.WritePropertyName("y");
js.Write (t.hexCoord.y);
js.WriteObjectEnd();
//End of Key
// Value
js.WritePropertyName("value");
js.WriteObjectStart();
js.WritePropertyName("tileType");
js.Write(t.spriteType.ToString());
js.WritePropertyName("penalty");
js.Write(t.penalty.ToString());
js.WritePropertyName("visibility");
js.Write(t.visibility.ToString());
js.WriteObjectEnd();
// End of Value
js.WriteObjectEnd();
}
js.WriteArrayEnd();
js.WritePropertyName("towns");
js.WriteArrayStart();
List<Map.Town> towns = m.GetTowns();
foreach (Map.Town t in towns) {
js.WriteObjectStart();
// Key
js.WritePropertyName("key");
js.WriteObjectStart();
js.WritePropertyName("x");
js.Write(t.hexCoord.x);
js.WritePropertyName("y");
js.Write (t.hexCoord.y);
js.WriteObjectEnd();
// End of Key
// Value
js.WritePropertyName("value");
js.WriteObjectStart();
js.WritePropertyName("playerSide");
js.Write(t.team);
js.WriteObjectEnd();
// End of Value
js.WriteObjectEnd();
}
js.WriteArrayEnd();
// TODO Units
js.WriteObjectEnd();
return js.ToString();
}
示例9: LoadWidgets
public void LoadWidgets(bool exclude_unless_post)
{
Debug.Assert(m_setup_complete, "You must call SetupComplete() before LoadWidgets()");
if (m_widgets_loaded) return; // already loaded - presumably by SetupComplete()
m_widgets_loaded = true;
// make request to backend to get widget HTML
JsonWriter w = new JsonWriter();
w.WriteObjectStart();
w.WritePropertyName("modules");
w.WriteArrayStart();
foreach (RemoteWidget wi in m_widgets.Values)
{
if (exclude_unless_post && wi.Method == "get") continue;
wi.DumpJson(w);
}
w.WriteArrayEnd(); // modules
w.WritePropertyName("global");
w.WriteObjectStart();
w.WritePropertyName("language");
w.Write(m_language);
if (m_have_user)
{
w.WritePropertyName("user");
w.WriteObjectStart();
w.WritePropertyName("namespace");
w.Write(m_user_namespace);
w.WritePropertyName("id");
w.Write(m_user_id);
w.WritePropertyName("login");
w.Write(m_user_login);
w.WritePropertyName("email");
w.Write(m_user_email);
w.WritePropertyName("url");
w.Write(m_user_url);
w.WritePropertyName("first_name");
w.Write(m_user_first_name);
w.WritePropertyName("last_name");
w.Write(m_user_last_name);
w.WritePropertyName("thumbnail_url");
w.Write(m_user_thumbnail_url);
w.WriteObjectEnd(); // user
}
w.WritePropertyName("items");
w.WriteArrayStart();
foreach (RemoteWidgetSubject s in m_items)
{
w.WriteObjectStart();
foreach (string k in s.Keys)
{
w.WritePropertyName(k);
object v = s[k];
if (v is int) w.Write((int)v);
else if (v is string) w.Write((string)v);
else throw new Exception("RemoteWidgetSubject values must be ints or strings");
}
w.WriteObjectEnd();
}
w.WriteArrayEnd();
w.WriteObjectEnd(); // global
w.WriteObjectEnd(); // outer
string json_request = w.ToString();
// now post the request to the backend server
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_remote_url);
req.Method = "POST";
req.ContentType = "application/x-javascript";
byte[] post_data = Encoding.UTF8.GetBytes(json_request);
req.ContentLength = post_data.Length;
string raw_data = "";
try
{
Stream post_stream = req.GetRequestStream();
post_stream.Write(post_data, 0, post_data.Length);
post_stream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader resp_stream = new StreamReader(resp.GetResponseStream());
/*
while (true)
{
string line = resp_stream.ReadLine();
if (line == null) break;
Debug.Print("line from response: " + line);
}
*/
raw_data = resp_stream.ReadToEnd();
resp.Close();
}
catch (WebException e)
{
SetError("Error communicating with widget server: " + e.Message);
return;
//.........这里部分代码省略.........
示例10: ToJson
public string ToJson()
{
var jsWriter = new JsonWriter();
jsWriter.WriteObjectStart();
jsWriter.WritePropertyName("alg");
jsWriter.Write(Algorithm.ToString());
if (null != KeyUri)
{
switch (KeyFormat)
{
case KeyFormat.Json:
jsWriter.WritePropertyName("jku");
break;
case KeyFormat.X509:
jsWriter.WritePropertyName("xku");
break;
case KeyFormat.Rfc4050:
jsWriter.WritePropertyName("xdu");
break;
}
jsWriter.Write(KeyUri.ToString());
}
if (false == string.IsNullOrEmpty(KeyId))
{
jsWriter.WritePropertyName("kid");
jsWriter.Write(KeyId);
}
jsWriter.WriteObjectEnd();
return jsWriter.ToString();
}
示例11: Report
private static void Report(string category, JsonWriter json, bool logFailure)
{
json.WriteObjectEnd();
try
{
string gameKey, secretKey;
GetKeys(out gameKey, out secretKey);
string jsonMessage = json.ToString();
string url = GetApiUrl(gameKey, category);
string auth = GetAuthorization(jsonMessage, secretKey);
string result = Post(url, jsonMessage, auth);
MyTrace.Send(TraceWindow.Analytics, jsonMessage);
}
catch (Exception ex)
{
m_enabled = false;
if (logFailure)
{
// Do not write it to log as classic exception (it would also make false reports for error reporter)
MySandboxGame.Log.WriteLine("Sending analytics failed: " + ex.Message);
}
}
}
示例12: UpdateIssue
public void UpdateIssue(IssueUpdate update, NetworkCredential credential,
Action<string> stdout, Action<string> stderr)
{
var client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credential.UserName + ":" + credential.Password)));
var jsonRPCQuery = new JsonWriter();
jsonRPCQuery.WriteObjectStart();
jsonRPCQuery.WritePropertyName("method");
jsonRPCQuery.Write("ticket.update");
jsonRPCQuery.WritePropertyName("params");
jsonRPCQuery.WriteArrayStart();
jsonRPCQuery.Write(update.Issue.Id);
jsonRPCQuery.Write(update.Comment);
jsonRPCQuery.WriteObjectStart();
jsonRPCQuery.WritePropertyName("action");
jsonRPCQuery.Write("resolve");
jsonRPCQuery.WritePropertyName("action_resolve_resolve_resolution");
jsonRPCQuery.Write("fixed");
jsonRPCQuery.WriteObjectEnd();
jsonRPCQuery.Write(true);
jsonRPCQuery.WriteArrayEnd();
jsonRPCQuery.WriteObjectEnd();
client.UploadString(this.RPCUrl(), jsonRPCQuery.ToString());
}
示例13: DownloadIssues
public Action DownloadIssues(string project, int start, bool includeClosedIssues,
Func<IEnumerable<Issue>, bool> onData,
Action<DownloadProgressChangedEventArgs> onProgress,
Action<bool, Exception> onCompleted)
{
Debug.Assert(project != null);
Debug.Assert(onData != null);
var client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.UploadStringCompleted += (sender, args) =>
{
if (args.Cancelled || args.Error != null)
{
if (onCompleted != null)
onCompleted(args.Cancelled, args.Error);
return;
}
JsonData data = JsonMapper.ToObject(args.Result);
if (data["error"] != null)
{
if (onCompleted != null)
onCompleted(false, new Exception((string)data["error"]["message"]));
return;
}
else if (data["result"] != null && data["result"].Count > 0)
{
var client2 = new WebClient();
client2.Headers.Add("Content-Type", "application/json");
var jsonRPCQuery = new JsonWriter();
jsonRPCQuery.WriteObjectStart();
jsonRPCQuery.WritePropertyName("method");
jsonRPCQuery.Write("system.multicall");
jsonRPCQuery.WritePropertyName("params");
jsonRPCQuery.WriteArrayStart();
for (int i = 0; i < data["result"].Count - 1; i++)
{
jsonRPCQuery.WriteObjectStart();
jsonRPCQuery.WritePropertyName("method");
jsonRPCQuery.Write("ticket.get");
jsonRPCQuery.WritePropertyName("params");
jsonRPCQuery.WriteArrayStart();
jsonRPCQuery.Write((int)data["result"][i]);
jsonRPCQuery.WriteArrayEnd();
jsonRPCQuery.WriteObjectEnd();
}
client2.UploadStringCompleted += (sender2, args2) =>
{
if (args2.Cancelled || args2.Error != null)
{
return;
}
int lastObjectStart = 0;
int count = 0;
bool inQuote = false;
bool lastWasBackslash = false;
for (int n = 1; n < args2.Result.Length; n++)
{
if (inQuote)
{
if (args2.Result[n] == '"' && !lastWasBackslash)
{
inQuote = false;
}
else if (args2.Result[n] == '\\')
{
lastWasBackslash = !lastWasBackslash;
}
else
{
lastWasBackslash = false;
}
}
else
{
if (args2.Result[n] == '"')
{
inQuote = true;
}
else if (args2.Result[n] == '}')
{
--count;
if (count == 0)
{
n++;
JsonData issueData = JsonMapper.ToObject(args2.Result.Substring(lastObjectStart, n - lastObjectStart));
TracIssue[] issues = new TracIssue[1];
issues[0] = new TracIssue();
issues[0].Id = (int)issueData["result"][0];
if (issueData["result"][3].ToString().Contains("milestone"))
{
issues[0].Milestone = (string)issueData["result"][3]["milestone"];
}
issues[0].Owner = (string)issueData["result"][3]["owner"];
//.........这里部分代码省略.........
示例14: Serialize
public static string Serialize()
{
var dto = new MusicDTO.EditData();
dto.BPM = EditData.BPM.Value;
dto.maxBlock = EditData.MaxBlock.Value;
dto.offset = EditData.OffsetSamples.Value;
dto.name = Path.GetFileNameWithoutExtension(EditData.Name.Value);
var sortedNoteObjects = EditData.Notes.Values
.Where(note => !(note.note.type == NoteTypes.Long && EditData.Notes.ContainsKey(note.note.prev)))
.OrderBy(note => note.note.position.ToSamples(Audio.Source.clip.frequency, EditData.BPM.Value));
dto.notes = new List<MusicDTO.Note>();
foreach (var noteObject in sortedNoteObjects)
{
if (noteObject.note.type == NoteTypes.Single)
{
dto.notes.Add(ToDTO(noteObject));
}
else if (noteObject.note.type == NoteTypes.Long)
{
var current = noteObject;
var note = ToDTO(noteObject);
while (EditData.Notes.ContainsKey(current.note.next))
{
var nextObj = EditData.Notes[current.note.next];
note.notes.Add(ToDTO(nextObj));
current = nextObj;
}
dto.notes.Add(note);
}
}
var jsonWriter = new JsonWriter();
jsonWriter.PrettyPrint = true;
jsonWriter.IndentValue = 4;
JsonMapper.ToJson(dto, jsonWriter);
return jsonWriter.ToString();
}
示例15: ExportPrettyPrint
public void ExportPrettyPrint()
{
OrderedDictionary sample = new OrderedDictionary ();
sample["rolling"] = "stones";
sample["flaming"] = "pie";
sample["nine"] = 9;
string expected = @"
{
""rolling"" : ""stones"",
""flaming"" : ""pie"",
""nine"" : 9
}";
JsonWriter writer = new JsonWriter ();
writer.PrettyPrint = true;
JsonMapper.ToJson (sample, writer);
Assert.AreEqual (expected, writer.ToString (), "A1");
writer.Reset ();
writer.IndentValue = 8;
expected = @"
{
""rolling"" : ""stones"",
""flaming"" : ""pie"",
""nine"" : 9
}";
JsonMapper.ToJson (sample, writer);
Assert.AreEqual (expected, writer.ToString (), "A2");
}