Parse a fixed width file with an array of objects? - filehelpers

Let's say I have a this fixed length string:
ABBCCC10purple crayon5 red pencil9 green marker
The A, B, and C fields are easily mapped:
[FixedLengthRecord]
public class OutterRecord
{
[FieldFixedLength(1)]
public string AField { get; set; }
[FieldFixedLength(2)]
public string BField { get; set; }
[FieldFixedLength(3)]
public string CField { get; set; }
}
However, the remaining part of the line is an array of objects. For example, let's say 10purple crayon is:
[FixedLengthRecord]
public class WritingInstrument
{
[FieldFixedLength(2)]
public string Count { get; set; }
[FieldFixedLength(7)]
public string Color { get; set; }
[FieldFixedLength(6)]
public string TypeOfInstrument { get; set; }
}
Is there a way in FileHelpers to parse the WritingIntsturments section? Given the constraint/limitation that each individual WritingInstrument record is a max of 15 characters wide, and there can be at most 10 items in the array.
I want the resulting deserialized object to look like this:
[FixedLengthRecord]
public class OutterRecord
{
[FieldFixedLength(1)]
public string AField { get; set; }
[FieldFixedLength(2)]
public string BField { get; set; }
[FieldFixedLength(3)]
public string CField { get; set; }
[SomeTypeOfFixedArray(10)]
List<WritingInstrument> WritingInstruments { get; set; }
}

You can just declare a field of type string[].
Here's a working program:
[FixedLengthRecord()]
public class OutterRecord
{
[FieldFixedLength(1)]
public string AField { get; set; }
[FieldFixedLength(2)]
public string BField { get; set; }
[FieldFixedLength(3)]
public string CField { get; set; }
[FieldFixedLength(15)]
public string[] WritingInstruments { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
var engine = new FixedFileEngine<OutterRecord>();
var records = engine.ReadString("ABBCCC10purple crayon5 red pencil9 green marker");
Assert.AreEqual("A", records[0].AField);
Assert.AreEqual("BB", records[0].BField);
Assert.AreEqual("CCC", records[0].CField);
Assert.AreEqual("10purple crayon", records[0].WritingInstruments[0]);
Assert.AreEqual("5 red pencil", records[0].WritingInstruments[1]);
Assert.AreEqual("9 green marker", records[0].WritingInstruments[2]);
Console.WriteLine("All OK");
Console.ReadKey();
}
}

Related

Save data on local storage

I am developing an app using Xamarin Forms PCL.
I want to save this class (I use this class as List in code)
public class TagInformation {
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string name { get; set; }
public bool isJob { get; set; }
public string _color { get; set; }
[Ignore]
public Color color { get; set; }
[Ignore]
public JobInformation jobInformation { get; set; }
public TagInformation(Color c, string n) {
color = c;
_color = c.ToHex();
name = n;
isJob = false;
}
public TagInformation() {
}
public void setColor() {
color = Color.FromHex(_color);
}
}
public class JobInformation {
public string shopName { get; set; }
public int wage { get; set; }
public bool isHoliday { get; set; }
public int holidayWage { get; set; }
public bool isMid { get; set; }
public int midnightWage { get; set; }
public bool[] holidayFlgs { get; set; }
public DateTime midStart { get; set; }
public DateTime midEnd { get; set; }
public int tranceportCost { get; set; }
public int closeDay { get; set; }
public int payMentDay { get; set; }
public TimeSpan shortRest { get; set; }
public TimeSpan longRest { get; set; }
public TimeSpan shortRestTime { get; set; }
public TimeSpan longRestTime { get; set; }
public TimeSpan commuteTime { get; set; }
public JobInformation() {
/* do samething */
}
}
Using SQLite, this class has another class, so it doesn't work on.
Is there any other way?
Do you have any ideas?
You could try to use Xamarin.Essentials: Preferences to save it as a json string.
1.Install the Newtonsoft.Json nuget.
2.Serialize your class to json string and save it with a key.
TagInformation tarinformation = xxxxx;
string jsonString = JsonConvert.SerializeObject(tarinformation);
Preferences.Set("your_key", jsonString); //save the data
3.Retrieve a value from preferences and deserialize the string.
var myValue = Preferences.Get("my_key", "default_value");//get the data string
TagInformation infor = JsonConvert.DeserializeObject<TagInformation>(myValue); //deserialize to your class

json string downloaded with mising values c# webclient

I'm conding a rent car asp.net mvc application and I have a strange problem.
I used the skyscanner api to get the available cars list but the json string downloaded is always missing the attribute cars or there is cars that exist and are available.
string datacar = "";
String ch = "http://partners.api.skyscanner.net/apiservices/carhire/liveprices/v2/UK/eur/fr/" + ids + "/" + ide + "/" + dstart + "T" + tstart + "/" + dend + "T" + tend + "/30?apiKey=dh219253907683824017371755201462&userip=127.0.0.1";
Uri url = new Uri(ch);
// Uri url2 = new Uri("http://partners.api.skyscanner.net/apiservices/carhire/liveprices/v2/UK/eur/fr/27539733/27539733/2016-09-10T17:45/2016-09-17T17:45/30?apiKey=dh219253907683824017371755201462&userip=127.0.0.1");
CarModel model = new CarModel();
// for (int i = 0; i <= 20; i++)
// {
using (WebClient wc = new WebClient())
{
wc.Headers.Set("Content-Type", "application/json");
datacar = wc.DownloadString(url);
//datacar = wc.DownloadString(url2);
model = JsonConvert.DeserializeObject<CarModel>(datacar);
}
and here it is the carModel defintion
namespace carrent.Models
{
public class SubmittedQuery
{
public string market { get; set; }
public string currency { get; set; }
public string locale { get; set; }
public string pickup_place { get; set; }
public string dropoff_place { get; set; }
public string pickup_date_time { get; set; }
public string drop_off_date_time { get; set; }
public string driver_age { get; set; }
}
public class Image
{
public int id { get; set; }
public string url { get; set; }
}
public class CarClass
{
public int id { get; set; }
public int sort_order { get; set; }
public string name { get; set; }
}
public class DebugItem
{
public string type { get; set; }
public string title { get; set; }
public string content { get; set; }
}
public class CarModel
{
public SubmittedQuery submitted_query { get; set; }
public List<object> cars { get; set; }
public List<object> websites { get; set; }
public List<Image> images { get; set; }
public List<CarClass> car_classes { get; set; }
public List<DebugItem> debug_items { get; set; }
}
}
Ps: the strange thing in this problem is when I initialize an url (the url2) the code run perfectly
please guys help me to get the solution and thanks

Raven Db Join on simple List

I have this kind of structure stored in Raven DB
public class AAA
{
public int Id { get; set; }
public string Name { get; set; }
}
public class BBB
{
public int Id { get; set; }
public string Name { get; set; }
public List<int> AAAIds { get; set; }
}
and I'm trying to get a query to basically show something like:
public class AAA_BBB
{
public int AAB_Id { get; set; }
public string AAA_Name { get; set; }
public bool ContainsAtLeastOneBBB { get; set; }
}
I'm trying to do something like:
var AAA_BBB = session.Query().Include(a=>a.AAAIds.Select(a=>a))
You need something like:
session.Query<BBB>().Include<BBB,AAA>(x=>x.AAAIds)

Deserialize JSON object to C# object that contains lists

I what to use the Rest Sharp Deserialization but i don't know how to build the right class
The JSON :
{
"LIST_ID":34468,
"CREATED_PERSONAL_FIELDS":[],
"EXISTING_PERSONAL_FIELD_NAMES":[ ["41028","Test1"] , ["41029","Test2"] ]
}
I tried this class:
public class PersonalFieldsAddResponse
{
public string LIST_ID { get; set; }
public List<List<string>> SUBSCRIBERS_CREATED { get; set; }
public List<List<string>> BAD_PERSONAL_FIELDS { get; set; }
}
And this one:
public class PersonalFieldsAddResponse
{
public string LIST_ID { get; set; }
public List<string> SUBSCRIBERS_CREATED { get; set; }
public List<string> BAD_PERSONAL_FIELDS { get; set; }
}
The Deserialization does get me the LIST_ID but i always get NULL at SUBSCRIBERS_CREATED and BAD_PERSONAL_FIELDS.
Any help will be appreciated.
LOL,
it need to be :
public class PersonalFieldsAddResponse
{
public string LIST_ID { get; set; }
public List<List<string>> CREATED_PERSONAL_FIELDS { get; set; }
public List<List<string>> EXISTING_PERSONAL_FIELD_NAMES { get; set; }
}

Handling document relationships with T[] instead of T using RavenDB

RavenDB docs show how to deal with document relationships in this sample using Includes.
public class Order
{
public Product[] Items { get; set; }
public string CustomerId { get; set; }
public double TotalPrice { get; set; }
}
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public string[] Images { get; set; }
public double Price { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public short Age { get; set; }
public string HashedPassword { get; set; }
}
How would I deal with Includes or Live Projections if I don't want to include the customer using Includes/Live Projections but a list of products instead:
public class Order
{
public string[] ItemIds { get; set; }
public string CustomerId { get; set; }
public double TotalPrice { get; set; }
}
If I understand what you're asking, this should help. I blogged about it here:
http://inaspiralarray.blogspot.com/2012/03/keeping-domain-model-pure-with-ravendb.html
Does that help?