Json.Net gives an error when creating my custom JsonConverter - c#

I have a problem whereby I wish to generate a JSON field where the field name is known at runtime e.g.:
{ "known_at_run_time": ["test","test","test"] }
So I tried implementing it this way, yet whenever I run my unit test I get an error saying that that my custom JsonConverter cannot be created. Here is my code:
TermFilter.cs
public enum ExecutionType { plain, fielddata, #bool, and, or }
[JsonObject(MemberSerialization.OptIn)]
public class TermFilter
{
#region PROPERTIES
private JsonTuple query;
private ExecutionType execution;
private string _execution;
private bool _cache;
#endregion
#region CONSTRUCTOR
public TermFilter()
{
try
{
this.query = null;
this.Execution = ExecutionType.plain;
this.Cache = true;
}
catch(Exception)
{
throw;
}
}
public TermFilter(ExecutionType execution)
: this()
{
try
{
this.Execution = execution;
}
catch (Exception)
{
throw;
}
}
public TermFilter(ExecutionType execution, bool cache)
: this(execution)
{
try
{
this.Cache = cache;
}
catch (Exception)
{
throw;
}
}
public TermFilter(string field, string[] terms)
:this()
{
try
{
this.Query = new JsonTuple(field, new HashSet<string>(terms));
}
catch (Exception)
{
throw;
}
}
#endregion
#region GET/SET
//[JsonProperty(ItemConverterType = typeof(JsonTupleConverter))]
//[JsonProperty]
[JsonConverter( typeof(JsonTupleConverter) )]
public JsonTuple Query
{
get { return query; }
set { query = value; }
}
public ExecutionType Execution
{
get { return execution; }
set
{
execution = value;
_execution = value.ToString();
}
}
[JsonProperty(PropertyName = "execution")]
public string _Execution
{
get { return _execution; }
set { _execution = value; }
}
[JsonProperty(PropertyName = "_cache")]
public bool Cache
{
get { return _cache; }
set { _cache = value; }
}
#endregion
#region METHODS
public TermFilter AddTerm(string term)
{
try
{
if (!this.query.Data.Contains(term))
this.query.Data.Add(term);
return this;
}
catch (Exception)
{
throw;
}
}
public string ToJson()
{
try
{
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
settings.Converters.Add(new JsonTupleConverter(new Type[] { typeof(JsonTuple) }));
settings.NullValueHandling = NullValueHandling.Ignore;
return JsonConvert.SerializeObject(this, settings);
//return JsonConvert.SerializeObject(this, Formatting.None, new JsonTupleConverter(typeof(JsonTuple)));
//return JsonConvert.SerializeObject(this, Formatting.None, new JsonConverter[] { new JsonTupleConverter(typeof(JsonTuple)), });
}
catch (Exception)
{
throw;
}
}
#endregion
}
JsonTuple.cs
public class JsonTuple
{
#region PROPERTIES
private string field;
private HashSet<string> data;
#endregion
#region CONSTRUCTOR
public JsonTuple()
{
try
{
this.field = null;
this.data = null;
}
catch (Exception)
{
throw;
}
}
public JsonTuple(string field, HashSet<string> data)
{
try
{
this.field = field;
this.data = data;
}
catch (Exception)
{
throw;
}
}
#endregion
#region GET/SET
public string Field
{
get { return field; }
set { field = value; }
}
public HashSet<string> Data
{
get { return data; }
set { data = value; }
}
#endregion
#region METHODS
public string ToJson()
{
try
{
return JsonConvert.SerializeObject(this, Formatting.None, new JsonTupleConverter(typeof(JsonTuple)));
}
catch (Exception)
{
throw;
}
}
#endregion
}
JsonTupleConverter.cs
public class JsonTupleConverter : JsonConverter
{
private readonly Type[] _types;
public JsonTupleConverter(params Type[] types)
{
try
{
_types = types;
}
catch (Exception)
{
throw;
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
try
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else if (!_types.Any(_t => _t == value.GetType()))
{
serializer.Serialize(writer, value);
}
else
{
JsonTuple tuple = (JsonTuple)value;
if ((tuple != null) && (tuple.Field != null) && (tuple.Data != null))
{
JToken entityToken = null;
if (tuple.Data != null)
entityToken = JToken.FromObject(tuple.Data);
JObject o = new JObject();
o.AddFirst(new JProperty(tuple.Field, entityToken));
o.WriteTo(writer);
}
}
}
catch (Exception)
{
throw;
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return _types.Any(t => t == objectType);
}
}
Test.cs
[TestMethod]
public void TermFieldSertialization()
{
try
{
TermFilter filter = new TermFilter("test.field", new string[] {"test1", "test2", "test3"});
Assert.IsNotNull(filter);
string sampleJson = filter.ToJson();
Assert.IsNotNull(sampleJson);
}
catch (Exception)
{
throw;
}
}
What am I doing wrong? Any information will help.

First, try removing the [JsonConverter] attribute from the Query property of your TermFilter class. You don't need it because the Query property is a JsonTuple, and you are already passing an instance of your JsonTupleConverter to the JsonConvert.SerializeObject() method inside your ToJson() method, specifying that it can handle JsonTuples. This will get rid of the error.
However, there is still another issue. It seems that your intent is to get the Query property to serialize to JSON, but as things stand now this will not happen. This is because you have marked your TermFilter class with [JsonObject(MemberSerialization.OptIn)], and the Query property lacks a [JsonProperty] attribute to signal that you want that property to be included in the output. You will need to add [JsonProperty("query")] to fix that. Once you have done that, you should get the output you expect.
As an aside, you don't need to catch exceptions if you only intend to throw them again without doing anything else with them. I see that pattern everywhere in your code. Instead, just leave out the try/catch altogether; it does exactly the same thing and will make your code much more concise. Only catch an exception if you intend to handle it.

I think that your exception is occurring because JsonTupleConverter doesn't have a parameterless constructor.
public JsonTupleConverter() { }
If you add that, the error should go away, but your code might not work because then it'd probably be trying to use a converter without the types set up correctly.
Maybe you should just be serializing it as a dictionary? E.g.
var myDict = new Dictionary<string, List<string>>
{
{ "known_at_run_time", new List<string> { "test","test","test" } }
};
string ser = JsonConvert.SerializeObject(myDict);
// ser is {"known_at_run_time":["test","test","test"]}

Related

c# reference correct parameter type

In Worker.SendMail in the code below, I need to pass a reference of itself to the RetryEmailSend method but am unsure how to reference it. Any help would be appreciated.
public class Worker
{
public bool SendMail()
{
try
{
//implementation
}
catch (Exception ex)
{
RetryEmailSend(NEED REFERENCE TO THIS INSTANCE Worker.SendMail());
}
return true;
}
}
public static class Util
{
public static bool RetryAction(Action action)
{
if (action == null)
throw new ArgumentNullException("action");
try { action(); return true; }
catch
{
return false;
}
}
public static bool RetryEmailSend(NEED INPUT TYPE HERE)
{
return Util.RetryAction(() => INPUT TYPE);
}
}

xUnit test engine's InlineDataAttribute + optional method parameters

Is it possible to make xUnit test work when you don't specify optional parameter values in InlineDataAttribute?
Example:
[Theory]
[InlineData(1, true)] // works
[InlineData(2)] // error
void Test(int num, bool fast=true){}
Yes it is. There are many ways to do it by redefining some original xunit attributes.
The following code is one of them, which would give you some idea.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class OptionalTheoryAttribute : TheoryAttribute
{
protected override IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo method)
{
var result = (List<ITestCommand>)base.EnumerateTestCommands(method);
try
{
return TransferToSupportOptional(result, method);
}
catch (Exception ex)
{
result.Clear();
result.Add(new LambdaTestCommand(method, () =>
{
throw new InvalidOperationException(
String.Format("An exception was thrown while getting data for theory {0}.{1}:\r\n{2}",
method.TypeName, method.Name, ex)
);
}));
}
return result;
}
private static IEnumerable<ITestCommand> TransferToSupportOptional(
IEnumerable<ITestCommand> testCommands, IMethodInfo method)
{
var parameterInfos = method.MethodInfo.GetParameters();
testCommands.OfType<TheoryCommand>().ToList().ForEach(
testCommand => typeof(TheoryCommand)
.GetProperty("Parameters")
.SetValue(testCommand, GetParameterValues(testCommand, parameterInfos)));
return testCommands;
}
private static object[] GetParameterValues(TheoryCommand testCommand, ParameterInfo[] parameterInfos)
{
var specifiedValues = testCommand.Parameters;
var optionalValues = GetOptionalValues(testCommand, parameterInfos);
return specifiedValues.Concat(optionalValues).ToArray();
}
private static IEnumerable<object> GetOptionalValues(TheoryCommand command, ParameterInfo[] parameterInfos)
{
return Enumerable.Range(command.Parameters.Length, parameterInfos.Length - command.Parameters.Length)
.ToList().Select(i =>
{
EnsureIsOptional(parameterInfos[i]);
return Type.Missing;
});
}
private static void EnsureIsOptional(ParameterInfo parameterInfo)
{
if (!parameterInfo.IsOptional)
{
throw new ArgumentException(string.Format(
"The parameter '{0}' should be optional or specified from data attribute.",
parameterInfo));
}
}
}
internal class LambdaTestCommand : TestCommand
{
private readonly Assert.ThrowsDelegate lambda;
public LambdaTestCommand(IMethodInfo method, Assert.ThrowsDelegate lambda)
: base(method, null, 0)
{
this.lambda = lambda;
}
public override bool ShouldCreateInstance
{
get
{
return false;
}
}
public override MethodResult Execute(object testClass)
{
try
{
lambda();
return new PassedResult(testMethod, DisplayName);
}
catch (Exception ex)
{
return new FailedResult(testMethod, ex, DisplayName);
}
}
}
public class OptionalTheoryTest
{
[OptionalTheory]
[InlineData(1)]
[InlineData(1, true)]
public void TestMethod(int num, bool fast = true)
{
// Arrange
// Act
// Assert
Assert.Equal(1, num);
Assert.True(fast);
}
}

SQL Datareader is not holding the values

I have been working with a project for last 4 months. We are using a custom framework for the development. The problem I am talking about, was working for all other classes. But for the first time I am facing this weird incident. Now Straight to break point.
My framework code is like
public static List<ViewNotSetBillableCoursesEntity> GetAllNotSetBillableCources()
{
try
{
List<ViewNotSetBillableCoursesEntity> entities = new List<ViewNotSetBillableCoursesEntity>();
string command = SELECT;
SqlConnection sqlConnection = MSSqlConnectionHandler.GetConnection();
SqlDataReader dataReader = QueryHandler.ExecuteSelect(command, sqlConnection);
entities = Maps(dataReader);
dataReader.Close();
return entities;
}
catch (Exception exception)
{
throw exception;
}
}
In the above method the dataReader is sent to Maps method.
The Maps method is ......
private static List<ViewNotSetBillableCoursesEntity> Maps(SqlDataReader theReader)
{
SQLNullHandler nullHandler = new SQLNullHandler(theReader);
// the incident is happening here, the SQLNullHandler is given below.
List<ViewNotSetBillableCoursesEntity> entities = null;
while (theReader.Read())
{
if (entities == null)
{
entities = new List<ViewNotSetBillableCoursesEntity>();
}
ViewNotSetBillableCoursesEntity entity = Mapper(nullHandler);
entities.Add(entity);
}
return entities;
}
The SQLNullHandler is given below:
puplic Class SQLNullHandler
{
private IDataReader _reader;
public SQLNullHandler(IDataReader reader)
{
_reader = reader;
}
#region Get Null value
public static object GetNullValue(int Value)
{
if(Value==0)
{
return null;
}
else
{
return Value;
}
}
public static object GetNullValue(double Value)
{
if (Value == 0)
{
return null;
}
else
{
return Value;
}
}
public static object GetNullValue(decimal Value)
{
if (Value == 0)
{
return null;
}
else
{
return Value;
}
}
public static object GetNullValue(DateTime Value)
{
if(DateTime.MinValue==Value)
{
return null;
}
else
{
return Value;
}
}
public static object GetNullValue(string Value)
{
if(Value.Length<=0)
{
return null;
}
else
{
return Value;
}
}
#endregion
public IDataReader Reader
{
get{return _reader;}
}
public bool IsNull(int index)
{
return _reader.IsDBNull(index);
}
public int GetInt32(int i)
{
return _reader.IsDBNull(i)? 0 : _reader.GetInt32(i);
}
public byte GetByte(int i)
{
return _reader.IsDBNull(i)? (byte)0 : _reader.GetByte(i);
}
//and so on for all possible type for this app
}
The Funny thing is for all these classes these methods and lines of code work very fine, but in this scenario after the line SQLNullHandler nullHandler = new SQLNullHandler(theReader); the datareder becomes empty.
My questions are
Why is this Happening and next,
what can be done to solve this problem?

Object Duplication/Serialization problem

I'm having a bit of a problem with a data serialization method I'm using to copy objects. Here's the method:
public static class ObjectDuplicator
{
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("the Type must be serializable.", "source");
}
if (Object.ReferenceEquals(source, null)) //dont try to serialize a null object
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
The problem is this: when I call this method using the code below
public void AddJob(Job job)
{
if (!Jobs.Contains(job))
{
Job newcopy = Utilities.ObjectDuplicator.Clone<Job>(job);
Jobs.Add(newcopy);
}
}
it throws this exception:
System.InvalidCastException was unhandled
Message=Unable to cast object of type 'KH.CharacterClasses.Freelancer' to type 'KH.CharacterClasses.Job'
Now, the type of job I'm adding is an inherited class from Job, (Freelancer) and the code for those two classes is below
[Serializable]
public class Job : Ability
{
protected JobCommand basecommand1;
protected JobCommand basecommand2;
protected JobCommand basecommand3;
protected JobCommand basecommand4;
protected JobCommand command1;
protected JobCommand command2;
protected JobCommand command3;
protected JobCommand command4;
bool mastered;
protected FFJob job;
protected string name;
int level;
public FFJob SetJob
{
get
{
return job;
}
}
public bool Mastered
{
get
{
return mastered;
}
}
public JobCommand Command1
{
get
{
return command1;
}
set
{
command1 = value;
}
}
public JobCommand DefaultCommand1
{
get
{
return basecommand1;
}
}
public JobCommand Command2
{
get
{
return command2;
}
set
{
command2 = value;
}
}
public JobCommand DefaultCommand2
{
get
{
return basecommand2;
}
}
public JobCommand Command3
{
get
{
return command3;
}
set
{
command3 = value;
}
}
public JobCommand DefaultCommand3
{
get
{
return basecommand3;
}
}
public JobCommand Command4
{
get
{
return command4;
}
set
{
command4 = value;
}
}
public JobCommand DefaultCommand4
{
get
{
return basecommand4;
}
}
public Job(string name, string description, int jobID)
: base(name, description, jobID, -1, -1, null, null, -1, -1)
{
}
public static bool operator ==(Job job1, Job job2)
{
if (System.Object.ReferenceEquals(job1, job2))
return true;
if (((object)job1 == null) || ((object)job2 == null))
return false;
return (job1.Name == job2.Name && job1.UID == job2.UID);
}
public static bool operator !=(Job job1, Job job2)
{
return !(job1 == job2);
}
// public abstract void CharacterModifier(BaseCharacter character);
// public abstract void CharacterDemodifier(BaseCharacter character);
}
[Serializable]
public class Freelancer : Job
{
public Freelancer()
: base("Freelancer", "A character not specializing in any class. Can combine the power of all mastered Jobs.", Globals.JobID.ID)
{
basecommand1 = JobCommand.Attack;
basecommand2 = JobCommand.Free;
basecommand3 = JobCommand.Free;
basecommand4 = JobCommand.Items;
command1 = basecommand1;
command2 = basecommand2;
command3 = basecommand3;
command4 = basecommand4;
job = FFJob.Freelancer;
}
}
I'm a bit stumped here because I know the ObjectDuplicator method does work. In fact, this code HAS worked before, but that was on a different computer, and I haven't looked at it in awhile. I'm a little stumped as to why the casting fails here. If someone could help me out with whats wrong thatd be great. If you need more details, just say what you need. I asked this question yesterday, but didn't get a workable answer.
Thanks
Try replacing
return (T)formatter.Deserialize(stream);
with
var result = formatter.Deserialize(stream);
for (Type now = result.GetType(); now != null; now = now.BaseType)
MessageBox.Show(now.FullName);
return result as T;
What does Clone<T> return? Do you see KH.CharacterClasses.Job in a list of base types? Seems like it's not the base type for Freelancer.
I would never place a return statement into a using clause! Do this instead:
object tClone = null;
using (Stream stream = new MemoryStream()) {
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
tClone = formatter.Deserialize(stream);
}
return (T)tClone;
If the exception is still thrown, then your types are indeed incompatible...
Figured out the solution here:
Casting Error when using serialization

There was an error reflecting type - XML Serialization issue

I have a Dictionary object which needs to be written into an XML file.
The dictionary contains String type as Key and a custom class's Object (Deriving from System.Windows.Forms.Control ) as Value.
namespace SharpFormEditorDemo
{
[Serializable]
public static class common
{
public static Dictionary<String,CommonControl > dicControls = new Dictionary<string, CommonControl>();
public static Object objSelected = new Object();
public static int ctrlId = 0;
//The serialization and Deserialization methods.
public static void Serialize(XmlTextWriter xmlTextWriter,Dictionary<String,CommonControl> dic)
{
xmlTextWriter.WriteStartDocument();
ControlSerializer file = new ControlSerializer(dic);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ControlSerializer));
xmlSerializer.Serialize(xmlTextWriter, file);
xmlTextWriter.WriteEndDocument();
}
}
The class CommonControl is like this
namespace SharpFormEditorDemo
{
public class CommonControl : System.Windows.Forms.Control
{
//private List<String> controls;
private String sql;
private int minVal; //Minimum value for a field
private int maxVal; //Maximum value for a field
private string displayValue; //Display Value
private string keyValue; //Key Value
private string clickEvent; //Click event
private string selectedIndexChangeEvent; //Combo box event.
private string validateEvent; //Validated event.
public string SelectedIndexChangeEvent
{
get { return selectedIndexChangeEvent; }
set { selectedIndexChangeEvent = value; }
}
public string ClickEvent
{
get { return clickEvent; }
set { clickEvent = value; }
}
public string ValidateEvent
{
get { return validateEvent; }
set { validateEvent = value; }
}
public string KeyValue
{
get { return keyValue; }
set { keyValue = value; }
}
public string DisplayValue
{
get { return displayValue; }
set { displayValue = value; }
}
public int MinVal
{
get { return minVal; }
set { minVal = value; }
}
public int MaxVal
{
get { return maxVal; }
set { maxVal = value; }
}
public String Sql
{
get { return sql; }
set { sql = value; }
}
//public List<String> Controls
//{
// get { return controls; }
// set { controls = value; }
//}
}
}
The class CommonControl is a deriving from Controls class.
What I want to do is to write the Above said dictionary to an XML file.
[Serializable]
public class ControlSerializer : ISerializable
{
public ControlSerializer()
{
}
private Dictionary<String, CommonControl> dicCtrl;
public Dictionary<String, CommonControl> DicCtrl
{
get { return dicCtrl; }
set { dicCtrl = value; }
}
public ControlSerializer(Dictionary<String, CommonControl> dic)
{
this.DicCtrl = dic;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException();
}
}
for that I'm using the ControlSerializer class
And in calling it like this
try
{
XmlTextWriter xlw = new XmlTextWriter(#"D:\Test.xml", null);
common.Serialize(xlw, common.dicControls);
}
catch (Exception exShow)
{
The problem is that I'm getting an exception saying
"There was an error reflecting type 'SharpFormEditorDemo.ControlSerializer'."
But I'm getting the type using typeof operator. Baffled on why this is happening. Sorry If I'm too lengthy but wanted to give the full picture.
Thanks
Generic dictionaries cannot be XmlSerialized. The error you get is caused by the public property DicCtrl.
Use the [XmlIgnore] attribute to skip this property when serializing (which is probably not what you want).
Change the type of the property to a type that can be serialized e.g. List<T>
Find or implement a serializable dictionary
Or implement IXmlSerializable
BTW the [Serializable] attribute is only needed for binary serialization. You do not need it for Xml serialization.
Guys.. With a little help on web I found a solution..
I had to add another class
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>: Dictionary<TKey, TValue>, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return;
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
Then used the SerializableDictionary object instead of normal Dictionary. Also in my CommonControls class I had to implement "IXmlSerializable" with following methods.
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(XmlWriter writer)
{
throw new NotImplementedException();
}
#endregion
Now the whole thing is working ok. Thanks Everyone. !!!
I think you'll find that Dictionary cannot be serialized with XmlSerializer
I used DataContractSerializer from System.Runtime.Serialization.dll. Serialized/deserialized my class with two Dictionary properties without any questions.

Categories

Resources