Keep application-specific fields synced with database - c#

I have a GameConsole model :
public class GameConsole
{
public int ID { get; protected set; } = 0;
public string Identifier { get; protected set; } = "GameConsole";
public List<string> ValidInputs { get; set; } = new List<string>(8);
public Dictionary<string, InputAxis> InputAxesMap { get; protected set; } = new Dictionary<string, InputAxis>(8);
public Dictionary<string, InputButton> InputButtonMap { get; protected set; } = new Dictionary<string, InputButton>(32);
public string ValidInputsStr { get; set; } = string.Empty;
public string InputAxesMapStr { get; set; } = string.Empty;
public string InputButtonMapStr { get; set; } = string.Empty;
}
I need ValidInputs, InputAxesMap, and InputButtonMap stored in an SQLite database. I've done this by serializing to JSON in their respective ValidInputsStr, InputAxesMapStr, and InputButtonMapStr fields.
However, I need to use these lists and dictionaries in the application. But each time I open the database context, ValidInputs, InputAxesMap, and InputButtonMap don't contain the data (unless I deserialize them from the JSON strings).
using (MyDatabaseContext context = new MyDatabaseContext(databasePath))
{
GameConsole gmConsole = context.Consoles.First((console) => console.Identifier == "GC");
Console.WriteLine(gmConsole.InputButtonMap.Count); //Prints 0
gmConsole.ReadInJSONFields(); //This is what I want to eliminate
Console.WriteLine(gmConsole.InputButtonMap.Count); //Has the proper data after reading from JSON
}
This is inconvenient considering there will be more objects in the future. How can I have everything be read and written directly to and from the database, and have these fields be populated?
An example GameConsole in JSON form:
{
"Identifier": "GC",
"ValidInputs": [
"left",
"up",
"a",
"b"
],
"InputAxesMap": {
"left": {
"AxisVal": 0,
"MinAxisVal": 0.0,
"MaxAxisVal": -1.0,
"MaxPercentPressed": 100
},
"up": {
"AxisVal": 1,
"MinAxisVal": 0.0,
"MaxAxisVal": -1.0,
"MaxPercentPressed": 100
},
"InputButtonMap": {
"a": {
"ButtonVal": 0
},
"b": {
"ButtonVal": 1
}
}
}

Related

Working with Irregular nodes/elements in json response from an API

I have a situation while working with a JSON response from an API. To give a background, I am consuming an API from a source using a REST API using 3.5 .net framework.
Below is the part of the JSON output and I am kind of struggling to use it.
a)
"value": {
"Description": "Total Calculated things",
"2018": "5,820,456 ",
"2019": "2,957,447 "
}
The last 2 elements are dynamic, those are tend to change in API response. I was expecting the format like I have mentioned below, but at this point of given time the source provider is not able to change it as the API is used in many other different programs. And Changing the things in the source API will make other program owners to change.
b)
"value": {
"Description": "Total Calculated EQUITY AND LIABILITIES",
"YearData": [ {
"Data": "5,820,456",
"Year": "2018"
},
{
"Data": "2,957,447 ",
"Year": "2019"
} ]
}
Is there any way to overcome such thing> Any way to convert a to b?
EDIT
#Xerillio , Thanks . How can I achieve the same using below JSON format.
var json = #"
{
""entityData"": [
{
""name"": ""Statement of Comprehensive Income"",
""subattrOutput"": [
{
""name"": ""Sales"",
""subattrOutput"": [],
""value"": {
""Description"": ""Sales "",
""2018"": ""8,704,888 "",
""2019"": ""4,760,717 ""
},
""score"": ""99.5"",
""valuetype"": ""object""
},
{
""name"": ""Cost of goods sold"",
""subattrOutput"": [],
""value"": {
""Description"": ""Cost of sales "",
""2018"": ""(6,791,489) "",
""2019"": ""(3,502,785) ""
},
""score"": ""99.75"",
""valuetype"": ""object""
}
],
""value"": null,
""score"": ""98.63"",
""valuetype"": ""object""
}
]
}";
I wish this was more easy, but i just got it here:
class Program
{
static async Task Main(string[] args)
{
string json1 = #"{""value"": {""Description"": ""Total Calculated things"",""2018"": ""5,820,456 "",""2019"": ""2,957,447 ""}}";
string json2 = #"{""value"": {""Description"": ""Total Calculated EQUITY AND LIABILITIES"",""YearData"": [ {""Data"": ""5,820,456"",""Year"": ""2018""},{""Data"": ""2,957,447 "",""Year"": ""2019""} ]}}";
var obj1 = JsonConvert.DeserializeObject<ObjectResult>(json1);
var obj2 = JsonConvert.DeserializeObject<ObjectResult>(json2);
var res1 = CastObject<Result1>(obj1.Value.ToString());
var res2 = CastObject<Result2>(obj2.Value.ToString());
var invalidRes1 = CastObject<Result1>(obj2.Value.ToString());
var invalidRes2 = CastObject<Result2>(obj1.Value.ToString());
Console.WriteLine(res1);
Console.WriteLine(res2);
}
public static T CastObject<T>(string obj)
{
return !TryCastObject(obj, out T result) ? default : result;
}
public static bool TryCastObject<TOut>(string objToCast, out TOut obj)
{
try
{
obj = JsonConvert.DeserializeObject<TOut>(objToCast);
return true;
}
catch
{
obj = default;
return false;
}
}
}
public class ObjectResult
{
public object Value { get; set; }
}
public class Result1
{
[JsonProperty(PropertyName = "description")] public string Description { get; set; }
[JsonProperty(PropertyName = "2018")] public string _2018 { get; set; }
[JsonProperty(PropertyName = "2019")] public string _2019 { get; set; }
}
public class Result2
{
[JsonProperty(PropertyName = "Description")] public string Description { get; set; }
[JsonProperty(PropertyName = "YearData")] public List<YearData> YearData { get; set; }
}
public class YearData
{
[JsonProperty(PropertyName = "Data")] public string Data { get; set; }
[JsonProperty(PropertyName = "Year")] public string Year { get; set; }
}
It is A LOT of work and sincerely, as there more nodes in the json i think that using JObject is the best option, and you will have to do a deserealization in more than 1 step :(
Depending on how large the JSON structure is you could deserialize it into a Dictionary and manually map it to the proper format:
var json = #"
{
""value"": {
""Description"": ""Total Calculated things"",
""2018"": ""5,820,456 "",
""2019"": ""2,957,447 ""
}
}";
var badObj = JsonSerializer.Deserialize<BadFormat>(json);
var result = new WrapperType
{
Value = new ProperlyFormattedType()
};
foreach (var pair in badObj.Value)
{
if (pair.Key == "Description")
{
result.Value.Description = pair.Value;
}
else if (int.TryParse(pair.Key, out int _))
{
result.Value.YearData
.Add(new DatedValues
{
Year = pair.Key,
Data = pair.Value
});
}
}
// Data models...
public class BadFormat
{
[JsonPropertyName("value")]
public Dictionary<string, string> Value { get; set; }
}
public class WrapperType
{
public ProperlyFormattedType Value { get; set; }
}
public class ProperlyFormattedType
{
public string Description { get; set; }
public List<DatedValues> YearData { get; set; } = new List<DatedValues>();
}
public class DatedValues
{
public string Year { get; set; }
public string Data { get; set; }
}
See an example fiddle here.

Convert csv file to json using C# with no header

I would like to convert CSV file to JSON using C#. I know that there are a lot of similar questions but I couldnĀ“t find something that could help me.
Source file looks like this:
2019-12-01T00:00:00.000Z;2019-12-10T23:59:59.999Z
50;false;2019-12-03T15:00:12.077Z;005033971003;48;141;2019-12-03T00:00:00.000Z;2019-12-03T23:59:59.999Z
100;false;2019-12-02T12:38:05.989Z;005740784001;80;311;2019-12-02T00:00:00.000Z;2019-12-02T23:59:59.999Z
First line is not header (actually I don't know how to call it - header usually have names of each property).
The result should look like this
{
"transactionsFrom": "2019-12-01T00:00:00.000Z","transactionsTo": "2019-12-10T23:59:59.999Z",
"transactions": [{
"logisticCode": "005033971003",
"siteId": "48",
"userId":"141",
"dateOfTransaction": "2019-12-03T15:00:12.077Z",
"price": 50
},
{
"logisticCode": "005729283002",
"siteId": "80",
"userId":"311",
"dateOfTransaction": "2019-12-02T12:38:05.989Z",
"price": 100
}]
}
I would like to use POCO - maybe something like this:
public class Headers
{
public string TransactionFrom { get; set; }
public string TransactionTo { get; set; }
}
public class Results
{
public string logisticCode { get; set; }
public string siteId { get; set; }
public string userId { get; set; }
public string dateOfTransaction { get; set; }
public string price { get; set; }
public string packSale { get; set; }
}
But the problem is I don't know how to continue. Maybe some example would help. I know I can use ChoETL, CsvHelper but I don't how.
This code might help you
Step1 - Create model class
public class Headers
{
public string TransactionFrom { get; set; }
public string TransactionTo { get; set; }
public List<Transaction> Transactions { get; set; }
}
public class Transaction
{
public string logisticCode { get; set; }
public string siteId { get; set; }
public string userId { get; set; }
public string dateOfTransaction { get; set; }
public string price { get; set; }
public string packSale { get; set; }
}
Step 2 - Split the file and read the records
string strInput = #"2019-12-01T00:00:00.000Z;2019-12-10T23:59:59.999Z
50;false;2019-12-03T15:00:12.077Z;005033971003;48;141;2019-12-03T00:00:00.000Z;2019-12-03T23:59:59.999Z
100;false;2019-12-02T12:38:05.989Z;005740784001;80;311;2019-12-02T00:00:00.000Z;2019-12-02T23:59:59.999Z";
var headers = new Headers();
var transactions = new List<Transaction>();
var csvrecords = strInput.Split(new[] { '\r', '\n' },StringSplitOptions.RemoveEmptyEntries);
int count = 1;
foreach(var record in csvrecords)
{
var values = record.Split(';');
if (count == 1)
{
headers.TransactionFrom = values[0];
headers.TransactionTo = values[1];
}
else
{
var transaction = new Transaction();
transaction.logisticCode = values[3].Trim();
transaction.siteId = values[4].Trim();
transaction.userId = values[5].Trim();
transaction.dateOfTransaction = values[2].Trim();
transaction.price = values[0].Trim();
transactions.Add(transaction);
}
count++;
}
headers.Transactions = transactions;
var jsonString = JsonConvert.SerializeObject(headers);
Console.WriteLine(jsonString);
Output -
{
"TransactionFrom": "2019-12-01T00:00:00.000Z",
"TransactionTo": "2019-12-10T23:59:59.999Z",
"Transactions": [
{
"logisticCode": "005033971003",
"siteId": "48",
"userId": "141",
"dateOfTransaction": "2019-12-03T15:00:12.077Z",
"price": "50",
"packSale": null
},
{
"logisticCode": "005740784001",
"siteId": "80",
"userId": "311",
"dateOfTransaction": "2019-12-02T12:38:05.989Z",
"price": "100",
"packSale": null
}
]
}
With Cinchoo ETL, you can do it as follows
Define class structures as below
public class Headers
{
public string TransactionFrom { get; set; }
public string TransactionTo { get; set; }
public List<Transaction1> Transactions { get; set; }
}
public class Transaction
{
[ChoFieldPosition(4)]
public string logisticCode { get; set; }
[ChoFieldPosition(5)]
public string siteId { get; set; }
[ChoFieldPosition(6)]
public string userId { get; set; }
[ChoFieldPosition(2)]
public string dateOfTransaction { get; set; }
[ChoFieldPosition(1)]
public string price { get; set; }
}
Parse the CSV, generate JSON as below
string csv = #"2019-12-01T00:00:00.000Z;2019-12-10T23:59:59.999Z
50;false;2019-12-03T15:00:12.077Z;005033971003;48;141;2019-12-03T00:00:00.000Z;2019-12-03T23:59:59.999Z
100;false;2019-12-02T12:38:05.989Z;005740784001;80;311;2019-12-02T00:00:00.000Z;2019-12-02T23:59:59.999Z";
string csvSeparator = ";";
using (var r = ChoCSVReader.LoadText(csv)
.WithDelimiter(csvSeparator)
.ThrowAndStopOnMissingField(false)
.WithCustomRecordSelector(o =>
{
string line = ((Tuple<long, string>)o).Item2;
if (line.SplitNTrim(csvSeparator).Length == 2)
return typeof(Headers);
else
return typeof(Transaction);
})
)
{
var json = ChoJSONWriter.ToTextAll(r.GroupWhile(r1 => r1.GetType() != typeof(Headers))
.Select(g =>
{
Headers master = (Headers)g.First();
master.Transactions = g.Skip(1).Cast<Transaction1>().ToList();
return master;
}));
Console.WriteLine(json);
}
JSON Output:
[
{
"TransactionFrom": "2019-12-01T00:00:00.000Z",
"TransactionTo": "2019-12-10T23:59:59.999Z",
"Transactions": [
{
"logisticCode": "005033971003",
"siteId": "48",
"userId": "141",
"dateOfTransaction": "false",
"price": "50"
}
{
"logisticCode": "005740784001",
"siteId": "80",
"userId": "311",
"dateOfTransaction": "false",
"price": "100"
}
]
}
]
I am not sure if i can help you with any codes as your source CSV is very confusing, but i'll try to give you some ideas that might work out.
Firstly, you don't need a model class. I mean, you can use it if you want, but seems unnecessary here.
Next up is reading the CSV file. As you haven't posted any codes related to that and also didn't mention any problem with reading the file, i assume you are reading the file properly. Reading the CSV and writing a JSON from it is relatively easy. However, the CSV file itself looks very confusing. How are you reading it tho? Are you reading it as plain text? Do you have column headers or atleast columns?
If you are reading the file as plain text, then i guess you only have one way. And that is splitting the string and construct a new string with the splitted values. Splitting should be relatively easy as you have ;(semi-colon) which is separating each column/data. So the basic idea is splitting the string and storing it in an array or list, something like this :
string[] values = myCSV.split(";");
Now all you need to do is, simply use the strings inside values to construct a new string. You can use the StringBuilder for that, or an easy way(not feasible tho) would be string concatenation. I personally would recommend you to go with the StringBuilder.
Guidelines:
StringBuilder in C#
Creating a new line in StringBuilder
Double quotes inside string
Hopefully this gives you some ideas.

How to work with nested Arrays and Objects using LINQ

so im working on a small project where i am consuming and deserialising json string into objects on C#. i set myself a business logic where i want to search for a team and return the number of goals they have scored (https://raw.githubusercontent.com/openfootball/football.json/master/2014-15/en.1.json)
The issue is i want to return the number of goals using LINQ instead of a loop (my original method). However, i do not know how i can retrieve the score. e.g
namespace ConsoleApp
{
class Program
{
private static string jsonUrl { get; set; } = "https://raw.githubusercontent.com/openfootball/football.json/master/2014-15/en.1.json";
private static string teamKey { get; set; } = "swansea";
static void Main()
{
var goal = Run(teamKey.ToLower());
Console.WriteLine(goal);
Console.ReadKey();
}
public static int Run(string team)
{
using (var webclient = new WebClient())
{
var rawJson = webclient.DownloadString(jsonUrl);
var jsonModel = JsonConvert.DeserializeObject<RootObject>(rawJson);
foreach (var rounds in jsonModel.rounds)
{
foreach (var match in rounds.matches)
{
var goal = match.team1.key.Equals(teamKey) ? match.score1 : 0;
if (goal == 0)
{
goal = match.team2.key.Equals(teamKey) ? match.score2 : 0;
}
return goal;
}
}
return 0;
}
}
}
public class Team1
{
public string key { get; set; }
public string name { get; set; }
public string code { get; set; }
}
public class Team2
{
public string key { get; set; }
public string name { get; set; }
public string code { get; set; }
}
public class Match
{
public string date { get; set; }
public Team1 team1 { get; set; }
public Team2 team2 { get; set; }
public int score1 { get; set; }
public int score2 { get; set; }
}
public class Round
{
public string name { get; set; }
public List<Match> matches { get; set; }
}
public class RootObject
{
public string name { get; set; }
public List<Round> rounds { get; set; }
}
}
The code above successfully executes and returns the correct number of goals based on the football team. but i dont think for performance this is the best way. (input: "swansea" expected result: 2 , Actual result: 2)
the array is represented as follows:
"rounds": [
{
"name": "Matchday 1",
"matches": [
{
"date": "2014-08-16",
"team1": {
"key": "manutd",
"name": "Manchester United",
"code": "MUN"
},
"team2": {
"key": "swansea",
"name": "Swansea",
"code": "SWA"
},
"score1": 1,
"score2": 2
},
{
"date": "2014-08-16",
"team1": {
"key": "leicester",
"name": "Leicester City",
"code": "LEI"
},
"team2": {
"key": "everton",
"name": "Everton",
"code": "EVE"
},
"score1": 3,
"score2": 5
}}]
This seems to require the from x in y ... select z syntax to obtain readable LINQ. Replace the part that starts with foreach (var rounds and ends with return 0; by the following code:
return (from round in jsonModel.rounds
from match in round.matches
let goal = match.team1.key.Equals(teamKey) ? match.score1 : 0
select goal == 0 ? (match.team2.key.Equals(teamKey) ? match.score2 : 0) : goal).FirstOrDefault();
I kept this as similar to the code in the question, for clarity. It would be better to extract helper methods to make the LINQ more readable: One helper for the expression match.team1.key.Equals(teamKey) ? match.score1 : 0, and one for the expression goal == 0 ? (match.team2.key.Equals(teamKey) ? match.score2 : 0) : goal.
The from x in y ... select z can be turned into a LINQ method chain. (E.g. the ReSharper tool can do that automatically.) But the result is so ugly there's no point in showing it.

Issue with accessing json nodes from C#

I have a json file that needs to be saved as sql server table. This is test.json that has Student details with coursework.
[{
"Studentid": "001006360",
"Grade": "2",
"ExtraWork": {
"TopRecommended": ["000133692",
"102067155",
"887273865"],
"OtherCourses": ["228963647",
"138909237",
"899791144",
"216165613",
"113239563"]
},
"Courses": [{
"smalldesc": "this is a test ",
"Details": {
"description": "Summary of the course",
"collegeCode": "32466"
}
},
{
"smalldesc": "Second test",
"Details": {
"description": "Business- Course Summary",
"collegeCode": "32469"
}
}]
}]
Below is the C# program.
I do not know how to access "smalldesc" and "collegeCode".
var jsonText = File.ReadAllText(#"C:\test.json");
var ser = JsonConvert.DeserializeObject<List<RootObject>>(jsonText);
for (int i = 0; i < ser.Count; i++)
{
string Studentid = ser[i].Studentid;
string Grade = ser[i].Grade;
var result = JsonConvert.DeserializeObject<List<Course>>(jsonText);
for (int k = 0; k < result.Count; k++)
{
string smalldesc = result[k].smalldesc;
string collegeCode = result[k].Details.collegeCode;
}
}
Json object class definition:
public class ExtraWork
{
public List<string> TopRecommended { get; set; }
public List<string> OtherCourses { get; set; }
}
public class Details
{
public string description { get; set; }
public string collegeCode { get; set; }
}
public class Course
{
public string smalldesc { get; set; }
public Details Details { get; set; }
}
public class RootObject
{
public string Studentid { get; set; }
public string Grade { get; set; }
public ExtraWork ExtraWork { get; set; }
public List<Course> Courses { get; set; }
}
what's the best way to save to sql server tables.
This line:
var ser = JsonConvert.DeserializeObject<List<RootObject>>(jsonText);
Is already doing all the deserialization for you, there is no need to call it again inside a loop.
Your code can be as simple as this:
var ser = JsonConvert.DeserializeObject<List<RootObject>>(jsonText);
foreach (var s in ser)
{
string Studentid = s.Studentid;
string Grade = s.Grade;
foreach(var course in ser.Courses)
{
string smalldesc = course .smalldesc;
string details = course .Details.collegeCode;
}
}
FYI: using a foreach loop is much simpler to work with when iterating a collection (assuming your collection type implements IEnumerable which most of the included collections will).

How to Get Values from a json response string in C#

I want to get "id" "gender", "name", "picture" from a this json response string
{
"data": [{
"name": "XXX",
"gender": "male",
"id": "528814",
"picture": {
"data": {
"is_silhouette": false,
"url": "https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-frc3\/v\/t1.0-1\/p50x50\/551182_10152227358459008__n.jpg?oh=983b70686285c2f60f71e665ace8ed5f&oe=54C1220C&__gda__=1422017140_998fbe013c4fe191ccadfdbc77693a76"
}
}
}
string[] data = friendsData.Split(new string[] { "}," }, StringSplitOptions.RemoveEmptyEntries).ToArray();
foreach (string d in data)
{
try
{
FacebookFriend f = new FacebookFriend
{
id = d.Substring("\"id\":\"", "\""),
gender = d.Substring("gender\":\"", "\""),
name = d.Substring("name\":\"", "\""),
picture = d.Substring("\"picture\":{\"data\":{\"url\":\"", "\"").Replace(#"\", string.Empty)
};
FacebookFriendList.Add(f);
}
catch
{
continue;
}
}
This code looks bad if the json data changes then you need to modify your logic accordingly. I would suggest you to serialize and deserialize the model data using Json serialization.
Model:
public class SerializationModel
{
public Data Data { get; set; }
}
public class Data
{
public string Name { get; set; }
public string Gender { get; set; }
public string Id { get; set; }
public Picture Picture { get; set; }
}
public class Picture
{
public PictureData Data { get; set; }
}
public class PictureData
{
public bool is_silhouette { get; set; }
public string url { get; set; }
}
Serialize your data to get the json output is like,
SerializationModel serializationModel = new SerializationModel
{
Data = new Data
{
Gender = "mALE",
Id = "88",
Name = "User",
Picture = new Picture
{
Data = new PictureData
{
is_silhouette = true,
url = "www.google.com"
}
}
}
};
string serializedString = Newtonsoft.Json.JsonConvert.SerializeObject(serializationModel);
which would yield the below result,
{"Data":{"Name":"User","Gender":"mALE","Id":"88","Picture":{"Data":{"is_silhouette":true,"url":"www.google.com"}}}}
To deserialize the json data back to the model,
SerializationModel sm = Newtonsoft.Json.JsonConvert.DeserializeObject<SerializationModel>(serializedString);
Then you can get the required values from the model itself.

Categories

Resources