OData paging with XmlSerializer - c#

I've managed to implement $inlinecount with WebApi.OData (v 4.0.0) using the ODataQueryOptions<T> and PageResult<T> classes like this:
POCO
public class Poco
{
public int id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
Controller
[ActionName("Default")]
public PageResult<Poco> Get(ODataQueryOptions<Poco> queryOptions)
{
var data = new Poco[] {
new Poco() { id = 1, name = "one", type = "a" },
new Poco() { id = 2, name = "two", type = "b" },
new Poco() { id = 3, name = "three", type = "c" },
new Poco() { id = 4, name = "four", type = "d" },
new Poco() { id = 5, name = "five", type = "e" },
new Poco() { id = 6, name = "six", type = "f" },
new Poco() { id = 7, name = "seven", type = "g" },
new Poco() { id = 8, name = "eight", type = "h" },
new Poco() { id = 9, name = "nine", type = "i" }
};
var t = new ODataValidationSettings() { MaxTop = 2 };
queryOptions.Validate(t);
var s = new ODataQuerySettings() { PageSize = 1 };
IQueryable results = queryOptions.ApplyTo(data.AsQueryable(), s);
var next = Request.GetNextPageLink();
var count = Request.GetInlineCount();
return new System.Web.Http.OData.PageResult<Poco>(
results as IEnumerable<Poco>, next, count);
}
I'm getting error 406 when I switch from JSON to old school XmlSerializer. Does anyone know if this should work?
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
GlobalConfiguration.Configuration.Formatters.Remove(
GlobalConfiguration.Configuration.Formatters.JsonFormatter);

PageResult can't be serialized by XmlSerializer because it doesn't have a public, parameterless constructor. But there's nothing stopping you from defining your own similar type that does have a public, parameterless constructor. It should be pretty simple to do. I'd recommend taking a look at the source code for PageResult and adopting a similar approach.

Related

Convert Flat Data from C# to JSON

I have a c# class that looks something like this:
public class Item
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
}
and i would like to convert it to json where json property name is item name and its value is item description. if some item has any children then i would like the json property name to stay as item name and the children to be added like item name:item description(the parent item description and type become empty string if it has any children, except when the type is array). The type has following values: array, string, int, object. if the item type is an array then each time a child is added to the type array item, the child is item description. so i would like those values to be added to the json array as well.
if the type is string or int the json property value should be int or string.
I am trying to write custom JsonSerializer but i am getting nowhere.
so if i have a list with items:
List<Item> MyItems = new List<Item>()
{
new Item { Id = 1, ParentId = null, Name = "Name1", Description = "", Type = "" },
new Item { Id = 2, ParentId = 1, Name = "Name2", Description = "Description1", Type = "String" },
new Item { Id = 3, ParentId = 1, Name = "Name3", Description = "", Type = "Array" },
new Item { Id = 4, ParentId = 3, Name = "", Description = "ArrayItem1", Type = "" },
new Item { Id = 5, ParentId = 3, Name = "", Description = "ArrayItem2", Type = "" },
new Item { Id = 6, ParentId = 3, Name = "", Description = "ArrayItem3", Type = "" },
new Item { Id = 7, ParentId = null, Name = "Name4", Description = "5", Type = "Int" },
};
then the json should like this:
{
"name1":{
"name2":"description1",
"name3":[
"ArrayItem1",
"ArrayItem2",
"ArrayItem1"
]
},
"name4":5
}
Here's an extension method that I think does what you want:
public static class Ex
{
public static string ToJson(this List<Item> items)
{
var lookup = items.ToLookup(x => x.ParentId);
JObject ToJson(int? parentId)
{
JProperty ToProperty(Item item)
{
switch (item.Type)
{
case "":
return new JProperty(item.Name, ToJson(item.Id));
case "String":
return new JProperty(item.Name, item.Description);
case "Array":
return new JProperty(item.Name, lookup[item.Id].Select(x => x.Description).ToArray());
case "Int":
return new JProperty(item.Name, int.Parse(item.Description));
default:
return new JProperty(item.Name);
}
}
return new JObject(lookup[parentId].Select(x => ToProperty(x)));
}
var output = ToJson(null);
var text = Newtonsoft.Json.JsonConvert.SerializeObject(output, Newtonsoft.Json.Formatting.Indented);
return text;
}
}
I run it like this:
List<Item> MyItems = new List<Item>()
{
new Item { Id = 1, ParentId = null, Name = "Name1", Description = "", Type = "" },
new Item { Id = 2, ParentId = 1, Name = "Name2", Description = "Description1", Type = "String" },
new Item { Id = 3, ParentId = 1, Name = "Name3", Description = "", Type = "Array" },
new Item { Id = 4, ParentId = 3, Name = "", Description = "ArrayItem1", Type = "" },
new Item { Id = 5, ParentId = 3, Name = "", Description = "ArrayItem2", Type = "" },
new Item { Id = 6, ParentId = 3, Name = "", Description = "ArrayItem3", Type = "" },
new Item { Id = 7, ParentId = null, Name = "Name4", Description = "5", Type = "Int" },
};
Console.WriteLine(MyItems.ToJson());
And I get this out:
{
"Name1": {
"Name2": "Description1",
"Name3": [
"ArrayItem1",
"ArrayItem2",
"ArrayItem3"
]
},
"Name4": 5
}

(List<Dictionary<Object, Object>> in Linq to extract data

I have a data definition
I Deserialize JSON to this object
#return is JSON
JsonConvert.DeserializeObject<List<Dictionary<Object, Object>>>(utils.RemoveJsonOuterClass("GetTable", JsonConvert.DeserializeObject(#return).ToString()));
olist = [
[{
"item": 1
"Name "One"
}],
[{
"item": 2
"Name "Two"
}],
[{
"item": 1
"Name "One Two"
}]
];
This is a List<Dictionary<Object, Object>>
I need to find all of the items where "item" == 1.
Can I Use Linq? or is there any other way while using a large amount of data?
First: Your json is not correct fix that.
A colon should be present between Name and value.
A comma should be present after item value
and then change your code as below
//Create a class matching response object
public class ResponseItem
{
[JsonProperty("item")]
public int Item { get; set; }
public string Name { get; set; }
}
var responseJson = utils.RemoveJsonOuterClass("GetTable",
JsonConvert.DeserializeObject(#return).ToString();
var responseData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<ResponseItem, ResponseItem>>>(responseJson);
Then use foreach with Where and apply condition
foreach (var responseObject in responseData.Where(x=>x.First().Item.Equals(1)))
{
}
Where is deferred execution and on each loop, it returns an object.
Here is the screenshot of my local execution.
Don't know if u're right with the object type. But the task is easy to solve:
static void Main(string[] args)
{
// Build the object
List<Dictionary<int, TestObject>> list = new List<Dictionary<int, TestObject>>();
// fill it with dictionaries
list.Add(new List<TestObject>()
{
new TestObject(){ Id = 1, Name = "One" },
new TestObject() { Id = 2, Name = "Two" },
new TestObject() { Id = 3, Name = "Three" }
}.ToDictionary(d => d.Id));
list.Add(new List<TestObject>()
{
new TestObject() { Id = 2, Name = "Two" },
new TestObject() { Id = 3, Name = "Three" }
}.ToDictionary(d => d.Id));
list.Add(new List<TestObject>()
{
new TestObject(){ Id = 1, Name = "One" },
new TestObject() { Id = 2, Name = "Two" }
}.ToDictionary(d => d.Id));
// Let's build a single list to work with
IEnumerable<TestObject> completeList = list.SelectMany(s => s.Values);
// aaaand filter it
IEnumerable<TestObject> filteredList = completeList.Where(l => l.Id == 1);
}
public class TestObject
{
public int Id { get; set; }
public string Name { get; set; }
}
Most part is initialization ;-)

finding common elements between two list using lambda expression

I have following two list:
var firstList = new List<ProgramInfo> ()
{
new ProgramInfo {Name = "A", ProgramId = 1, Description = "some text1"},
new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text2"},
new ProgramInfo {Name = "D", ProgramId = 3, Description = "some text3"},
new ProgramInfo {Name = "E", ProgramId = 4, Description = "some text4"}
};
var secondList = new List<ProgramInfo> ()
{
new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text1"},
new ProgramInfo {Name = "D", ProgramId = 3, Description = "some text2"},
};
this two list gets generated at runtime and I have to select the common ProgramInfo depending on the program id from both of this list
for example, in case of above example the output should be
var thirdList = new List<ProgramInfo>()
{
new ProgramInfo {Name = "C", ProgramId = 2, Description = "some text1"},
new ProgramInfo {Name = "D", ProgramId = 3, Description = "some text2"},
};
public class ProgramInfo
{
public string Name { get; set; }
public int ProgramId { get; set; }
public string Description { get; set; }
}
Can someone suggest me how can I do this using lambda expression?
Use Linq .Intersect. For that to work your class needs to override the Equals and GetHashCode
var thirdList = firstList.Intersect(secondList);
You can also specify an IEqualityComparer instead of overriding the functions:
public class Comparer : IEqualityComparer<ProgramInfo>
{
public bool Equals(ProgramInfo x, ProgramInfo y)
{
return x.Name == y.Name &&
x.ProgramId == y.ProgramId &&
x.Description == y.Description;
}
public int GetHashCode(ProgramInfo obj)
{
return obj.Name.GetHashCode() ^
obj.ProgramId.GetHashCode() ^
obj.Description.GetHashCode();
}
}
var thirdList = firstList.Intersect(secondList, new Comparer());

Can we initialize an object/collection using LINQ in JSON-like syntax?

Can we do something like this or something similar?
var staffs = {
"Staff":
[
{ "ID" : 1, "Name" : "John" },
{ "ID" : 2, "Name" : "Mark"}
]
};
foreach (var staff in staffs)
{
Console.WriteLine("ID: {0}", staff.ID);
Console.WriteLine("Name: {0}", staff.Name);
}
Instead of this long code:
public class Test
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
List<Staff> staffs = new List<Staff>()
{
new Staff() {ID = 1, Name = "John"},
new Staff() {ID = 2, Name = "Mark"}
};
foreach (var staff in staffs)
{
Console.WriteLine("ID: {0}", staff.ID);
Console.WriteLine("Name: {0}", staff.Name);
}
}
}
public class Staff
{
public int ID { get; set; }
public string Name { get; set; }
}
I tried but Visual Studio shows syntax error.
You can make it shorter but not exactly like JSON.
List<Staff> staffs = new List<Staff>()
{
new Staff() {ID = 1, Name = "John"},
new Staff() {ID = 2, Name = "Mark"}
};
...can you turn into: (This is an array not a List<T> but you can wrap it)
var staffs = new[]
{
new Staff {ID = 1, Name = "John"},
new Staff {ID = 2, Name = "Mark"}
};
Nope. What you've demonstrated is the allowed syntax for collection and object initializers.
If you're trying to shorten it somewhat you could drop the trailing () off of each initializer, such as:
var list = new List<Staff>
{
new Staff {ID = 1, Name = "John"},
new Staff {ID = 2, Name = "Mark"}
}
The closest you can get is:
var staffs = new List<Staff> {
new Staff{ ID : 1, Name : "John" },
new Staff{ ID : 2, Name : "Mark"}
};
foreach (var staff in staffs)
{
Console.WriteLine("ID: {0}", staff.ID);
Console.WriteLine("Name: {0}", staff.Name);
}
Note: member names and values are not quoted, but strongly typed (unlike json).
You might be able to deserialize a JSON representation directly to the object graph using a Json serializer.

Adding data to ObservableCollection in WPF

I have some problem here. Here it is:
I have this class
public class NewsFeedResources
{
public string Name { get; set; }
public string Id { get; set; }
public string Message { get; set; }
public static ObservableCollection<NewsFeedResources> _newsfeed = new ObservableCollection<NewsFeedResources>
{
new NewsFeedResources { Name = "Joe", Id = "1", Message="Foo" },
new NewsFeedResources { Name = "Wandy", Id = "2", Message="Bar" },
new NewsFeedResources { Name = "Yuliana", Id = "3", Message="Baz" },
new NewsFeedResources { Name = "Hardi", Id = "4", Message="Baz" },
};
public static ObservableCollection<NewsFeedResources> newsFeedResources
{ get { return _newsfeed; }
}
}
If I have another data such as
Name=John, Id=5, Message="Stack overflow"
Name=Jane, Id=6, Message="Hello world"
How can I add the data into the class, but not from the constructor? Thanks for the help
ObservableCollection exposes the Collection<T>.Add Method:
Adds an object to the end of the Collection.
So you'd have:
_newsfeed.Add(new NewsFeedResources {Name = "John",
Id = 5,
Message = "Stack overflow"});
_newsfeed.Add(new NewsFeedResources {Name = "Jane",
Id = 6,
Message = "Hello world"});
(typed from memory)
call a function from constructor or anywhere as u like and add items like below
NewsFeedResources NFR=new NewsFeedResources(){Name=John, Id=5, Message="Stack overflow"};
_newsfeed.add(NFR);
NewsFeedResources NFR1 =new NewsFeedResources(){Name=Jane, Id=6, Message="Hello world"};
_newsfeed.add(NFR);

Categories