json.net deserialize string to nested class - c#

I'm receiving a Json string back from a http request that looks something like this:
{
"info":
[
{
"calls":0,
"errors":"[error1, error2, error3]",
"messages":0,
"mail":3
}
],
"received":5,
"valid":3
}
The entity I'm trying to deserialize into is structured about the same
class ResponseEntity
{
private Info info;
private int received;
private int valid;
[JsonProperty("info")]
public Info Info
{
get { return info; }
set { info = value; }
}
[JsonProperty("valid")]
public int valid
{
get { return valid; }
set { valid = value; }
}
[JsonProperty("received")]
public int received
{
get { return received; }
set { received = value; }
}
public class Info
{
private int calls;
private List<string> errors;
private int messages;
private int mail;
[JsonProperty("calls")]
public int Calls
{
get { return calls; }
set { calls = value; }
}
[JsonProperty("messages")]
public int Messages
{
get { return messages; }
set { messages = value; }
}
[JsonProperty("errors")]
public List<string> Errors
{
get { return errors; }
set { errors = value; }
}
[JsonProperty("mail")]
public int Mail
{
get { return mail; }
set { mail = value; }
}
}
}
When I try to deserialize it though I'm getting an exception
ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;
Cannot deserialize JSON array into type 'CSharpRestService.ResponseEntity+Info'.
Can anybody see what I'm doing wrong? I'm thinking the 'errors' json key is messing things up, but I also tried a string array.

My test code would not compile with the nested Info class (due to a property naming conflict) so I removed it from within the ResposeEntity class.
Along with this I fixed some issues with your JSON (your info object was an array and the strings in your errors array needed to be in quotes).
see below:
JSON
{
info":
{
"calls":0,
"errors":["error1", "error2", "error3"],
"messages":0,
"mail":3
},
"received":5,
"valid":3
}
Classes
class ResponseEntity
{
private Info info;
private int received;
private int valid;
[JsonProperty("info")]
public Info Info
{
get { return info; }
set { info = value; }
}
[JsonProperty("valid")]
public int Valid
{
get { return valid; }
set { valid = value; }
}
[JsonProperty("received")]
public int Received
{
get { return received; }
set { received = value; }
}
}
public class Info
{
private int calls;
private List<string> errors;
private int messages;
private int mail;
[JsonProperty("calls")]
public int Calls
{
get { return calls; }
set { calls = value; }
}
[JsonProperty("messages")]
public int Messages
{
get { return messages; }
set { messages = value; }
}
[JsonProperty("errors")]
public List<string> Errors
{
get { return errors; }
set { errors = value; }
}
[JsonProperty("mail")]
public int Mail
{
get { return mail; }
set { mail = value; }
}
}
Test Code
string json = "{\"info\":{\"calls\":0,\"errors\":[\"error1\", \"error2\", \"error3\"],\"messages\":0,\"mail\":3},\"received\":5,\"valid\":3}";
ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;
Hope this helps.

Related

Neo4jClient Node/Relationship Class conventions

Is there a standard naming convention for the properties/methods of a node/relationship class when working with Neo4jClient?
I'm following this link Neo4jClient - Retrieving relationship from Cypher query to create my relationship class
However, there are certain properties of my relationship which i can't get any value despite the relationship having it. While debugging my code, i realized certain properties was not retrieved from the relationship when creating the relationship object.
this is my relationship class
public class Creates
{
private string _raw;
private int _sourcePort;
private string _image;
private int _DestinationPort;
private int _eventcode;
private string _name;
private string _src_ip;
private int _src_port;
private string _dvc;
private int _signature_ID;
private string _dest_ip;
private string _computer;
private string _sourceType;
private int _recordID;
private int _processID;
private DateTime _time;
private int _dest_port;
public string Raw { get { return _raw; } set { _raw = value; } }
public int SourcePort { get { return _sourcePort; } set { _sourcePort = value; } }
public string Image { get { return _image; } set { _image = value; } }
public int DestinationPort { get { return _DestinationPort; } set { _DestinationPort = value; } }
public int Eventcode { get { return _eventcode; } set { _eventcode = value; } }
public string Name { get { return _name; } set { _name = value; } }
public string Src_ip { get { return _src_ip; } set { _src_ip = value; } }
public int Src_port { get { return _src_port; } set { _src_port = value; } }
public string DVC { get { return _dvc; } set { _dvc = value; } }
public int Signature_ID { get { return _signature_ID; } set { _signature_ID = value; } }
public string Dest_ip { get { return _dest_ip; } set { _dest_ip = value; } }
public string Computer { get { return _computer; } set { _computer = value; } }
public string SourceType { get { return _sourceType; } set { _sourceType = value; } }
public int RecordID { get { return _recordID; } set { _recordID = value; } }
public int ProcessID { get { return _processID; } set { _processID = value; } }
public DateTime Indextime { get { return _time; } set { _time = value; } }
public int Dest_port { get { return _dest_port; } set { _dest_port = value; } }
}
This is another class
public class ProcessConnectedIP
{
public Neo4jClient.RelationshipInstance<Pivot> bindto { get; set; }
public Neo4jClient.Node<LogEvent> bindip { get; set; }
public Neo4jClient.RelationshipInstance<Pivot> connectto { get; set; }
public Neo4jClient.Node<LogEvent> connectip { get; set; }
}
This is my neo4jclient query to get the relationship object
public IEnumerable<ProcessConnectedIP> GetConnectedIPs(string nodeName)
{
try
{
var result =
this.client.Cypher.Match("(sourceNode:Process{name:{nameParam}})-[b:Bind_IP]->(bind:IP_Address)-[c:Connect_IP]->(connect:IP_Address)")
.WithParam("nameParam", nodeName)
.Where("b.dest_ip = c.dest_ip")
.AndWhere("c.Image=~{imageParam}")
.WithParam("imageParam", $".*" + nodeName + ".*")
.Return((b, bind, c, connect) => new ProcessConnectedIP
{
bindto = b.As<RelationshipInstance<Creates>>(),
bindip = bind.As<Node<LogEvent>>(),
connectto = c.As<RelationshipInstance<Creates>>(),
connectip = connect.As<Node<LogEvent>>()
})
.Results;
return result;
}catch(Exception ex)
{
Console.WriteLine("GetConnectedIPs: Error Msg: " + ex.Message);
return null;
}
}
This is the method to read the results
public void MyMethod(string name)
{
IEnumerable<ProcessConnectedIP> result = clientDAL.GetConnectedIPs(name);
if(result != null)
{
var results = result.ToList();
Console.WriteLine(results.Count());
foreach (ProcessConnectedIP item in results)
{
Console.WriteLine(item.Data.Src_ip);
Console.WriteLine(item.bindto.StartNodeReference.Id);
Console.WriteLine(item.bindto.EndNodeReference.Id);
Console.WriteLine(item.connectto.StartNodeReference.Id);
Console.WriteLine(item.connectto.EndNodeReference.Id);
Node<LogEvent> ans = item.bindip;
LogEvent log = ans.Data;
Console.WriteLine(log.Name);
Node<LogEvent> ans1 = item.connectip;
LogEvent log1 = ans1.Data;
Console.WriteLine(log1.Name);
}
}
}
Somehow, i'm only able to populate the relationship object with src_ip/src_port/dest_ip/dest_port values. the rest are empty.
Is there any possible reason why? I've played with upper/lower cases on the properties names but it does not seem to work.
This is the section of the graph im working with
This is the relationship properties sample:
_raw: Some XML dataSourcePort: 49767Image: C:\Windows\explorer.exeDestinationPort: 443EventCode: 3Name: Bind
IPsrc_ip: 172.10.10.104dvc: COMPUTER-NAMEsrc_port:
49767signature_id: 3dest_ip: 172.10.10.11Computer:
COMPUTRE-NAME_sourcetype:
XmlWinEventLog:Microsoft-Windows-Sysmon/OperationalRecordID:
13405621ProcessId: 7184_time: 2017-08-28T15:15:39+08:00dest_port: 443
I'm not entirely sure how your Creates class is ever populated, in particular those fields - as your Src_port property doesn't match the src_port in the sample you provided (case wise).
I think it's probably best to go back to a super simple version. Neo4jClient will map your properties to the properties in the Relationship as long as they have the same name (and it is case-sensitive).
So start with a new Creates class (and use auto properties - it'll make your life a lot easier!)
public class Creates
{
public string Computer { get; set; }
}
Run your query with that and see if you get a result, then keep on adding properties that match the name and type you expect to get back (int, string etc)
It seems that i have to give neo4j node/relationship property names in lowercase and without special characters at the start of the property name, in order for the above codes to work.
The graph was not created by me at the start thus i had to work on it with what was given. I had to get the developer who created the graph to create the nodes with lowercases in order for the above to work.

Null error when assigning value to property

Trying to create a custom object that will allow me to pickup errors in the records.
public class gridIntegerField
{
private int value;
private bool isValid;
private string message;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
public bool IsValid
{
get { return isValid; }
set { isValid = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}
}
public class gridRecord
{
private gridIntegerField printRun;
public gridIntegerField PrintRun
{
get { return printRun; }
set { printRun = value; }
}
}
when creating object and trying to set the values i get the folowing error...
An unhandled exception of type 'System.NullReferenceException' occurred in XML- Console.exe
Code for creating object...
gridRecord spr = new gridRecord();
spr.PrintRun.Value = 200;
spr.PrintRun.IsValid = true;
spr.PrintRun.Message = "No Errors";
Console.WriteLine(spr.PrintRun.Value.ToString());
Console.WriteLine(spr.PrintRun.IsValid.ToString());
Console.WriteLine(spr.PrintRun.Message.ToString());
Console.ReadKey();
the error happens at this line of code
spr.PrintRun.Value = 200;
Because you do not instantiate the printRun field after gridRecord instantiating.
You can do it in the constructor method.
public class gridRecord
{
private gridIntegerField printRun;
public gridIntegerField PrintRun
{
get { return printRun; }
set { printRun = value; }
}
//Add a constructor method
public gridRecord()
{
//and instantiate the printRun.
printRun = new gridIntegerField();
}
}

C# XmlSerializer has error while reflecting type

To transfer data, I'm using XmlSerializer. But I get a runtime error at following line:
XmlSerializer serializer = new XmlSerializer(typeof(Packets.Wrapper));
The error is "Additional information: Error reflecting type 'Packets.Wrapper'.". MSDN says that I have to use an empty contructor, but it doesn't fix the error.
The class looks like this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Packets
{
[Serializable]
public class Wrapper
{
int _sID = 0;
int _sSession = 0;
PacketType _sType = PacketType.None;
AuthRequest _sAuthRequest = null;
AuthResponse _sAuthResponse = null;
ProxyRequest _sProxyRequest = null;
ProxyResponse _sProxyResponse = null;
public Wrapper()
{
}
public int ID
{
get { return _sID; }
set { _sID = value; }
}
public int Session
{
get { return _sSession; }
set { _sSession = value; }
}
public PacketType Type
{
get { return _sType; }
set { _sType = value; }
}
public AuthRequest AuthRequest
{
get { return _sAuthRequest; }
set { _sAuthRequest = value; }
}
public AuthResponse AuthResponse
{
get { return _sAuthResponse; }
set { _sAuthResponse = value; }
}
public ProxyRequest ProxyRequest
{
get { return _sProxyRequest; }
set { _sProxyRequest = value; }
}
public ProxyResponse ProxyResponse
{
get { return _sProxyResponse; }
set { _sProxyResponse = value; }
}
}
[Serializable]
public enum PacketType
{
AuthRequest,
AuthResponse,
ProxyRequest,
ProxyResponse,
None
}
[Serializable]
public enum AuthResult
{
Accepted,
Denied,
Error
}
[Serializable]
public enum ProxyAction
{
Send,
Response,
Connect,
Close
}
[Serializable]
public enum ProxyResult
{
Connected,
Sent,
Revieved,
Error
}
[Serializable]
public class AuthRequest
{
string username = null;
string password = null;
public AuthRequest()
{
}
public string Username
{
get { return username; }
set { username = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
[Serializable]
public class AuthResponse
{
AuthResult authResult = AuthResult.Denied;
public AuthResponse()
{
}
public AuthResult AuthResult
{
get { return authResult; }
set { authResult = value; }
}
}
[Serializable]
public class ProxyRequest
{
ProxyAction _sAction;
string _sIP = null;
int _sPort = 0;
TcpClient _sClient = null;
public ProxyRequest()
{
}
public ProxyAction Action
{
get { return _sAction; }
set { _sAction = value; }
}
public string IP
{
get { return _sIP; }
set { _sIP = value; }
}
public int Port
{
get { return _sPort; }
set { _sPort = value; }
}
public TcpClient Client
{
get { return _sClient; }
set { _sClient = value; }
}
}
[Serializable]
public class ProxyResponse
{
public ProxyResult _sResult = ProxyResult.Error;
public StringBuilder _sError = new StringBuilder();
public StringBuilder _sRecieved = new StringBuilder();
public TcpClient _sClient = null;
public ProxyResponse()
{
}
public ProxyResult Result
{
get { return _sResult; }
set { _sResult = value; }
}
public StringBuilder Error
{
get { return _sError; }
set { _sError = value; }
}
public StringBuilder Recieved
{
get { return _sRecieved; }
set { _sRecieved = value; }
}
public TcpClient Client
{
get { return _sClient; }
set { _sClient = value; }
}
}
}
I hope you can help me and thank you for your time.
I got it.
TcpClient isn't serializable.

JsonConvert.SerializeObject() fails in serializing class with DateTime fields

I'm having a class with only private fields and their public getter-setters. I need to convert class object into JSON String hence I'm using JSON.Net.
Following is a simple snippet to convert class object into a JSON string.
MyClass obj = new MyClass();
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
But the method SerializeObject throws StackOverflowException at field in MyClass of type DateTime. What's happening here?
Update
Following is how MyClass looks like (as it is, I don't mind sharing the actual class)
class MyClass
{
private int _Model;
public int Model
{
get
{
return _Model;
}
set
{
_Model = value;
}
}
private long _ProductionControlNumber;
public long ProductionControlNumber
{
get
{
return _ProductionControlNumber;
}
set
{
_ProductionControlNumber = value;
}
}
private DateTime _ProductionDate;
public DateTime ProductionDate
{
get
{
return _ProductionDate;
}
set
{
_ProductionDate = value;
}
}
private DateTime _TestDate;
public DateTime TestDate
{
get
{
return _TestDate;
}
set
{
_TestDate = value;
}
}
private DateTime _TestStartTime;
public DateTime TestStartTime
{
get
{
return _TestStartTime;
}
set
{
_TestStartTime = value;
}
}
private TimeSpan _TestDuration;
public TimeSpan TestDuration
{
get
{
return _TestDuration;
}
set
{
_TestDuration = value;
}
}
public DateTime TestEndTime
{
get
{
//TODO Perform start end time computing logic.
return TestEndTime;
}
}
private int _TestBed;
public int TestBed
{
get
{
return _TestBed;
}
set
{
_TestBed = value;
}
}
private long _EngineSerial;
public long EngineSerial
{
get
{
return _EngineSerial;
}
set
{
_EngineSerial = value;
}
}
private Single _FuelSpecificGravity;
public Single FuelSpecificGravity
{
get
{
return _FuelSpecificGravity;
}
set
{
_FuelSpecificGravity = value;
}
}
private long _FuelConsume100;
public long FuelConsume100
{
get
{
return _FuelConsume100;
}
set
{
_FuelConsume100 = value;
}
}
private long _FuelConsume110;
public long FuelConsume110
{
get
{
return _FuelConsume100;
}
set
{
_FuelConsume100 = value;
}
}
private int _TemporaryRPM;
public int TemporaryRPM
{
get
{
return _TemporaryRPM;
}
set
{
_TemporaryRPM = value;
}
}
private int _PermanentRPM;
public int PermanentRPM
{
get
{
return _PermanentRPM;
}
set
{
_PermanentRPM = value;
}
}
private Single _RatedPower;
public Single RatedPower
{
get
{
return _RatedPower;
}
set
{
_RatedPower = value;
}
}
private int _RatedSpeed;
public int RatedSpeed
{
get
{
return _RatedSpeed;
}
set
{
_RatedSpeed = value;
}
}
private double _PulleyDiameter;
public double PulleyDiameter
{
get
{
return _PulleyDiameter;
}
set
{
_PulleyDiameter = value;
}
}
private double _RopeDiameter;
public double RopeDiameter
{
get
{
return _RopeDiameter;
}
set
{
_RopeDiameter = value;
}
}
private Single _FullLoad;
public Single FullLoad
{
get
{
return _FullLoad;
}
set
{
_FullLoad = value;
}
}
}
Also, I'll have another class which will have MyClass type field (along with its own similar set of fields), which is going to be converted into JSON string too, and that shouldn't be a problem since JSON.Net is said to support that situation too.
Note: I'm new to C# but I've previously worked with JSON in Java, where I get to play with JSONObject and JSONArray, and they were pretty straight forward.
It looks like your TestEndTime property's getter references itself. Therefore when Json.NET tries to serialize it, it recursively accesses itself and causes the StackOverflowException.
Hope that helps!

What is the proper way to handle an array of one class in another class?

Here are two simple classes to illustrate my question:
class Widget
{
private int _widgetID;
public int WidgetID
{
get { return _widgetID; }
set { _widgetID = value; }
}
private int _categoryID;
public int CategoryID
{
get { return _categoryID; }
set { _categoryID = value; }
}
private string _widgetName;
public string WidgetName
{
get { return _widgetName; }
set { _widgetName = value; }
}
}
And them the second class:
class WidgetCategory
{
private int _widgetCategoryID;
public int WidgetCategoryID
{
get { return _widgetCategoryID; }
set { _widgetCategoryID = value; }
}
private Widget[] _widgets;
public Widget[] Widgets
{
get { return _widgets; }
set { _widgets = value; }
}
private string _widgetCategoryName;
public string WidgetCategoryName
{
get { return _widgetCategoryName; }
set { _widgetCategoryName = value; }
}
}
How would I handle this situation in the most efficient way?
Also, so you know, I will need to nest other classes the same way below the Widget class.
You should create a read-only property of type System.Collections.ObjectModel.Collection<Widget>.
Collection properties should be read only
Use Collection<T>

Categories

Resources