For deserialising a json object, I had to define a parent class that would contain an object or an array of objects for the child class. It has to be an object if an object was retrieved, or an array of objects if an array was retrieved from the json.
JSON array object
{"y":{"x":[{"data":28}, {"data":56}, {"data":89}]}}
JSON object
{"y":{"x":{"data":28}}}
y is receiving x at a time, and x[] at another time. There is no such condition to determine whether y would recieve an array or an object.
Hence for determining whether I received an array or not, I am checking the IsArray() condition.
I tried
class Y
{
public X x { get { return System.IsArray() ? new X() : new x[] }; set; }
}
class X
{
public int data { get; set; }
}
It isnt working.
System.IsArray() isn't being recognised??
First off, an array is an object. That's a good thing, since it allows these functions to work (both assume using System;):
bool IsArray(object o) { return o is Array; }
bool IsArray(object o) { return o.GetType().IsArray; }
Second, if you want a property whose type can be either X or X[], the property's type needs to be object:
class Y
{
private object _x;
public object x {
get { return _x; }
set
{
if (value.GetType != typeof(X) && value.GetType != typeof(X[]))
throw new ArgumentException("value");
_x = value;
}
}
}
This somewhat ignores the advantage of static typing, as you're using object and checking types at run time. It would really be much simpler to define the property as an array, even for those cases where there's only one value. In such cases, it would be an array whose length is 1.
I've been using the Json.NET Nuget package, and it's been really easy to work with:
string jsonStr = "{'y':{'x':[{'data':28}, {'data':56}, {'data':89}]}}";
dynamic jobject = JsonConvert.DeserializeObject(jsonStr);
bool isArray = jobject.y.x.Type == JTokenType.Array;
bool isObject = jobject.y.x.Type == JTokenType.Object;
Hope this helps!
the property x of Type X in class Y cannot be an array of X if you explicity state it is of type X. Declaring it as an object would be one way to get round this.
If you want to check it is an array I would use a backing field (say _x) and then where you use the property do a check (typeof(_x) == X[])
That could get messy though, my best advice would be set the type of property x to X[] and in the set determine whether the value was an array (if so just set _x = value) or if not add value to an empty array of X
You can try this...
if (obj.GetType().IsArray)
{
...
}
Another way you can do it is,
if (obj is Array) {
...
}
Related
Given json:
{ "x":"a", "x":"b" }
When deserializing into a C# dynamic object, an object is created with property x with value b. No JsonReaderException is thrown, even though the same property is sent twice.
Is there a way to detect if property is sent more than once in the json string?
The reason why no JsonReaderException is thrown is because duplicate keys are valid in JSON.
A quick and dirty workaround would be to have a counter that gets incremented every time X property on your class gets set. If it is > 1 then you know.
public class Test
{
private string _x;
private int xCounter = 0;
public string X
{
set
{
if(xCounter > 1) throw new Exception("DUPES");
xCounter++;
_x = value;
}
get
{
return _x;
}
}
}
This will obviously fail if you try to set X again after deserialization.
I have two viewState properties.
One is a List<MyObject> MyObjects
And another one is MyObject SelectedObject
Whenever user selects an object from the list, I assign it to SelectedObject like this:
SelectedObject = MyObjects.Where(x=>x.MyId ==IdSelectedInUI)
This allows me easy manipulation of the SelectedObject.
Now, When I change SelectedObject's properties, how Can I assign those properties back to the original object in the list? I was thinking to use ref, but cannot quite figure it out.
Edit
All of the objects are classes, not structs.
So, here's what I have:
//Children are of type =List<MyParentObject.ChildObject>
public MyParentObject MyParent
{
get
{
if (ViewState["MyParent"] == null)
{
MyParent= GetItemsFromDatabase();
}
return (TaskInfo)ViewState["MyParent"];
}
set
{
ViewState["MyParent"] = value;
}
}
public MyParentObject.ChildObject SelectedChild
{
get
{
return (MyParentObject.ChildObject)ViewState["Child"];
}
set
{
ViewState["Child"] = value;
}
}
protected onButtonClick()
{
SelectedChild = MyParent.Children.Where(x=>x.Child_Id == Convert.ToInt32(txt.Text)).FirstOrDefault();
SelectedChild.Property2 = "test2" //OldValue "test1"
//now if I run
MyParent.Children.Where(x=>x.Child_Id ==Convert.ToInt32(txt.Text)).Select(x=>x.Property2) //Shows "test1" and I expect "test2"
}
}
SelectedObject is an object reference. It is identical with the original object in the list - SelectedObject and the "original" object are the same object. If you are seeing behavior that indicates otherwise, please update your question.
It depends. Assuming MyObject is a class, it is passed "by value", but the value being passed is a reference to the original object.
For instance:
void Mutate(SomeObject x)
{
x.val = "banana";
}
void Reassign(SomeObject x)
{
x = new SomeObject();
x.val = "Garbage";
}
public static void Main()
{
SomeObject x = new SomeObject();
x.val = "Apple";
Console.WriteLine(x.val); // Prints Apple
Mutate(x);
Console.WriteLine(x.val); // Prints banana
Reassign(x);
Console.WriteLine(x.val); // Still prints banana
}
This is how passing objects works in C#. You can modify the contents, but not the "value" of the object itself (which is a pointer to it in memory).
If you are looking to change the contents of the object, nothing special needs to be done.
Passing an object by ref parameter will allow you to reassign the reference to it. For example, if:
void Reassign(ref SomeObject x) // implementation
Was used in the previous example, the print out of the final WriteLine would have been "Garbage", as the reference itself was changed, and due to the ref parameter, this changed the reference in the caller as well.
+Edit
If MyObject is a struct, however, the structure is copied when passed, and reassigning it inside a different scope will have no effect on the original structure.
What you could do for this is to have a function return the structure and assign it:
MyStruct Mutate(MyStruct x){}
// calling
myStruct = Mutate(myStruct);
Or do it by reference parameter.
Let me explain my situation. I have a program who reads an external connection and gives me an array of integers (or booleans). Those inputs should feed an object that has some properties (X, Y, Z, for example). So, if a read a value on array, i should write those values in the properties. Is there a way to pass those values by ref (for example) ? Thinking logically , the best way way would be pointers (property X pointing to array[0]), but these aren't very unclear to me.
I can create a way to look for changes in array (but is a very large array, +60000), then update my object. But i think this would be a bad ideia.
Sorry if i wrote any crap, i'm just starting on C#.
Some pseudo code to help.
class obj
{
int X {get; set;}
public obj(ref int x)
{
X = x;
}
}
class main
{
void main()
{
int a;
obj test = new obj(ref a);
}
}
So if: a = 10, obj.X = 10 too.
public class MyClass
{
private int[] backingArray;
public int X
{
get
{
if (backingArray == null)
return -1;
else
return backingArray[0];
}
}
public MyClass(int[] array)
{
if (array.Length > 0)
backingArray = array;
}
}
class Main
{
void Main()
{
int[] array = new int[] { 2 };
MyClass test = new MyClass(array);
array[0] = 6;
Console.WriteLine(test.X);//prints 6
}
}
Of course this only works with reference types (arrays are reference types). If you wanted to do this whole thing with a value type, you'd need to "wrap" it in some reference type. You can use a class such as the following to wrap anything if you don't have anything convenient.
public class Wrapper<T>
{
public T Value { get; set; }
}
It's not possible to use ref in the manor that you've shown in the OP. You wouldn't be able to store the value that was passed by reference. If you could, then you could end up passing some value on the stack and then having the created object that holds the reference living longer than the item on the stack. If that happened you would end up with a reference it a location in memory that no longer holds the variable you intended. This was somewhat of a gotcha in C++ that the designers of C# went out of their way to ensure can't happen (at least not without a lot of work).
*Solved. Thanks for the explanations guys, I didn't fully understand the implications of using a value type in this situation.
I have a struct that I'm using from a static class. However, the behavior is showing unexpected behavior when I print it's internal state at runtime. Here's my struct:
public struct VersionedObject
{
public VersionedObject(object o)
{
m_SelectedVer = 0;
ObjectVersions = new List<object>();
ObjectVersions.Add(o);
}
private int m_SelectedVer;
public int SelectedVersion
{
get
{
return m_SelectedVer;
}
}
public List<object> ObjectVersions;//Clarifying: This is only used to retrieve values, nothing is .Added from outside this struct in my code.
public void AddObject(object m)
{
ObjectVersions.Add(m);
m_SelectedVer = ObjectVersions.Count - 1;
}
}
Test code
VersionedObject vo = new VersionedObject(1);
vo.AddObject(2);//This is the second call to AddObject()
//Expected value of vo.SelectedVerion: 1
//Actual value of vo.SelectedVersion: 1
Now, if you test this code in isolation, i.e., copy it into your project to give it a whirl, it will return the expected result.
The problem; What I'm observing in my production code is this debug output:
objectName, ObjectVersions.Count:2, SelectedVer:0,
Why? From my understanding, and testing, this should be completely impossible under any circumstances.
My random guess is that there is some sort of immutability going on, that for some reason a new struct is being instanced via default constructor, and the ObjectVersions data is being copied over, but the m_SelectedVersion is private and cannot be copied into the new struct?
Does my use of Static classes and methods to manipulate the struct have anything to do with it?
I'm so stumped I'm just inventing wild guesses at this point.
Struct is value type. So most likely you are creating multiple copies of your object in your actual code.
Consider simply changing struct to class as content of your struct is not really good fit for value type (as it is mutable and also contains mutable reference type).
More on "struct is value type":
First check FAQ which have many good answers already.
Value types are passed by value - so if you call function to update such object it will not update original. You can treat them similar to passing integer value to function: i.e. would you expect SomeFunction(42) to be able to change value of 42?
struct MyStruct { public int V;}
void UpdateStruct(MyStruct x)
{
x.V = 42; // updates copy of passed in object, changes will not be visible outside.
}
....
var local = new MyStruct{V = 13}
UpdateStruct(local); // Hope to get local.V == 42
if (local.V == 13) {
// Expected. copy inside UpdateStruct updated,
// but this "local" is untouched.
}
Why is this a struct and not a class? Even better, why are you tracking the size of the backing store (List<T>) rather than letting the List<T> track that for you. Since that underlying backing store is public, it can be manipulated without your struct's knowledge. I suspect something in your production code is adding to the backing store without going through your struct.
If it were me, I'd set it up something like this, though I'd make it a class...but that's almost certainly a breaking change:
public struct VersionedObject
{
public VersionedObject()
{
this.ObjectVersions = new List<object>() ;
return ;
}
public VersionedObject(object o) : this()
{
ObjectVersions.Add(o);
return ;
}
public VersionedObject( params object[] o ) : this()
{
ObjectVersions.AddRange( o ) ;
return ;
}
public int SelectedVersion
{
get
{
int value = this.ObjectVersions.Count - 1 ;
return value ;
}
}
public List<object> ObjectVersions ;
public void AddObject(object m)
{
ObjectVersions.Add(m);
return ;
}
}
You'll note that this has the same semantics as your struct, but the SelectedVersion property now reflects what's actually in the backing store.
Assuming I have a struct:
struct Vector
{
public int X, Y;
// ...
// some other stuff
}
and a class:
class Map
{
public Vector this[int i]
{
get
{
return elements[i];
}
set
{
elements[i] = value;
}
}
private Vector[] elements
// ...
// some other stuff
}
I want to be able to do something like: map[index].X = 0; but I can't, because the return value is not a variable.
How do I do this, if at all possible?
You should avoid mutable structs.
If you want your type to be mutable use a class instead.
class Vector
{
public int X { get; set; } // Use public properties instead of public fields!
public int Y { get; set; }
// ...
// some other stuff
}
If you want to use a struct, make it immutable:
struct Vector
{
private readonly int x; // Immutable types should have readonly fields.
private readonly int y;
public int X { get { return x; }} // No setter.
public int Y { get { return y; }}
// ...
// some other stuff
}
The compiler prevents you from doing this because the indexer returns a copy of an object not a reference (struct is passed by value). The indexer returns a copy, you modify this copy and you simply don't see any result. The compiler helps you avoid this situation.
If you want to handle such situation you should use class instead or change the way you deal with Vector. You shouldn't modify it's value but initialize it's values in constructor, more on this topic: Why are mutable structs “evil”?.
define Vector as class,
or
store value in a temporary variable
var v = map[index];
v.X = 0;
map[index] = v;
or
add function to change
map[index] = map[index].Offset()
or
let the [] operator return a setter class
class Setter { Vector[] Data; int Index; public double X { get { return Data[Index]; } set { Data[Index] = new Vector(value, Data[Index].Y); }}}
public Setter this[int i]
{
get
{
return new Setter() { Data = elements, Index= i };
}
}
Although generic classes work pretty well for many purposes, they do not provide any reasonable way to access structs by reference. This is unfortunate since in many cases a collection of structs would offer better performance (both reduced memory footprint and improved cache locality) and clearer semantics than a collection of class objects. When using arrays of structs, one can use a statement like ArrayOfRectangle[5].Width += 3; with very clear effect: it will update field X of ArrayOfRectangle[5] but it will not affect field X of any other storage location of type Rectangle. The only things one needs to know to be certain of that are that ArrayOfRectangle is a Rectangle[], and Rectangle is a struct with a public int field X. If Rectangle were a class, and the instance held in ArrayOfRectangle[5] had ever been exposed to the outside world, could be difficult or impossible to determine whether the instance referred to by ArrayOfRectangle[5] was also held by some other code which was expecting that field X of its instance wouldn't change. Such problems are avoided when using structures.
Given the way .net's collections are implemented, the best one can do is usually to make a copy of a struct, modify it, and store it back. Doing that is somewhat icky, but for structs that aren't too big, the improved memory footprint and cache locality achieved by using value types may outweigh the extra code to explicitly copy objects from and to the data structures. It will almost certainly be a major win compared with using immutable class types.
Incidentally, what I'd like to see would be for collections to expose methods like:
OperateOnElement<paramType>(int index, ref T element, ref paramType param, ActionByRef<T,paramType> proc) which would call proc with the appropriate element of the collection along with the passed-in parameter. Such routines could in many cases be called without having to create closures; if such a pattern were standardized, compilers could even use it to auto-generate field-update code nicely.