public void ParseNumberWithForcedFormat() { var reader = new JSonReader(); var result1 = reader.ReadAsJSonObject("[12, 13, 14]", JSonReaderNumberFormat.AsInt32); var result2 = reader.ReadAsJSonObject("[" + (uint.MaxValue + 1ul) + ", " + (uint.MaxValue + 2ul) + ", " + (uint.MaxValue + 3ul) + "]", JSonReaderNumberFormat.AsInt64); var result3 = reader.ReadAsJSonObject("[12.12, 13.13, 14.14]", JSonReaderNumberFormat.AsDouble); var result4 = reader.ReadAsJSonObject("[12.12, 13.13, 14.14]", JSonReaderNumberFormat.AsDecimal); var result5 = reader.Read("[12, 13, 14]", JSonReaderNumberFormat.AsInt32); var result6 = reader.Read("[" + (uint.MaxValue + 1ul) + ", " + (uint.MaxValue + 2ul) + ", " + (uint.MaxValue + 3ul) + "]", JSonReaderNumberFormat.AsInt64); var result7 = reader.Read("[12.12, 13.13, 14.14]", JSonReaderNumberFormat.AsDouble); var result8 = reader.Read("[12.12, 13.13, 14.14]", JSonReaderNumberFormat.AsDecimal); var result9 = reader.ReadAsJSonMutableObject("[12, 13, 14]", JSonReaderNumberFormat.AsInt32); var result10 = reader.ReadAsJSonMutableObject("[" + (uint.MaxValue + 1ul) + ", " + (uint.MaxValue + 2ul) + ", " + (uint.MaxValue + 3ul) + "]", JSonReaderNumberFormat.AsInt64); var result11 = reader.ReadAsJSonMutableObject("[12.12, 13.13, 14.14]", JSonReaderNumberFormat.AsDouble); var result12 = reader.ReadAsJSonMutableObject("[12.12, 13.13, 14.14]", JSonReaderNumberFormat.AsDecimal); Assert.IsNotNull(result1); Assert.IsNotNull(result2); Assert.IsNotNull(result3); Assert.IsNotNull(result4); Assert.IsNotNull(result5); Assert.IsNotNull(result6); Assert.IsNotNull(result7); Assert.IsNotNull(result8); Assert.IsNotNull(result9); Assert.IsNotNull(result10); Assert.IsNotNull(result11); Assert.IsNotNull(result12); }
/// <summary> /// Loads recorded set of bayeux messages from an input stream-reader. /// </summary> public int LoadRecording(TextReader input) { if (input == null) { throw new ArgumentNullException("input"); } int addedResponses = 0; var reader = new JSonReader(input, true); // if not yet end-of-the-input: while (input.Peek() != -1) { try { var jObject = reader.ReadAsJSonMutableObject(); if (jObject != null) { RecordBayeux("F-" + addedResponses, jObject); addedResponses++; } } catch (Exception ex) { DebugLog.WriteCoreLine("Invalid format of recorded bayeux responses"); DebugLog.WriteBayeuxException(ex); break; } } return(addedResponses); }
/// <summary> /// Records a response as bayeux message, while only 'data' part of the message is given. /// It will try to parse data and put as IJSonMutableObject, or if parsing fails, it will put it /// just as string value of the field. /// </summary> public RecordedDataSourceResponse RecordBayeuxData(string name, int internalStatus, string channel, string data) { if (string.IsNullOrEmpty(channel)) { throw new ArgumentNullException("channel"); } var response = new RecordedBayeuxDataSourceResponse(name); var bayeuxMessage = CreateEmptyBayeuxResponse(internalStatus, channel); if (string.IsNullOrEmpty(data)) { bayeuxMessage.SetValue("data", data); } else { try { var reader = new JSonReader(data); bayeuxMessage.SetValue("data", reader.ReadAsJSonMutableObject()); } catch (Exception ex) { DebugLog.WriteBayeuxLine("Parsing of 'data' for RecordedBayeuxDataSourceResponse failed!"); DebugLog.WriteBayeuxException(ex); bayeuxMessage.SetValue("data", data); } } response.AsJSon = bayeuxMessage; Record(response); return(response); }
/// <summary> /// Records a response as bayeux message. /// </summary> public RecordedBayeuxDataSourceResponse RecordBayeux(string name, string messageAsJSon) { var reader = new JSonReader(messageAsJSon); var response = new RecordedBayeuxDataSourceResponse(name); response.AsJSon = reader.ReadAsJSonMutableObject(); Record(response); return(response); }
public void ReadMutableJSonAndUpdateIt() { const string jsonText = "[{\"minimumVersion\":\"1.0\",\"type\": null,\"channel\":\"\\/meta\\/handshake\",\"supportedConnectionTypes\":[\"request-response\"],\"successful\":true}]"; var reader = new JSonReader(jsonText); var result = reader.ReadAsJSonMutableObject(); Assert.IsNotNull(result, "Invalid data read!"); Assert.AreEqual("1.0", result[0]["minimumVersion"].StringValue, "Expected other value for minimumVersion!"); result[0].AsMutable.SetValue("minimumVersion", "2.0"); Assert.AreEqual("2.0", result[0]["minimumVersion"].StringValue, "Expected updated value of minimumVersion!"); result[0].AsMutable.Remove("minimumVersion"); Assert.IsFalse(result[0].Contains("minimumVersion"), "Shoudn't contain the minimumVersion field!"); }
/// <summary> /// Get the value of given JSON object. /// </summary> T IJSonObject.ToObjectValue <T>() { if (typeof(T) == typeof(IJSonMutableObject)) { var reader = new JSonReader(_data); return((T)reader.ReadAsJSonMutableObject()); } if (typeof(T) == typeof(IJSonObject)) { var reader = new JSonReader(_data); return((T)reader.ReadAsJSonObject()); } return((T)JSonObjectConverter.ToObject(this, typeof(T))); }