I'm currently working on a minecraft-like game. All-terrain data is stored in SQLite. I have 3 tables that should be joined by one SQL Query using LINQ to List theMother. Tables are:
public class Chunks {
[PrimaryKey]
public int Id { get; set; }
[Indexed]
public double X { get; set; }
[Indexed]
public double Y { get; set; }
public int Resolution { get; set; }
public float Size { get; set; }
public bool isBackground { get; set; }
}
public class Cell{
[PrimaryKey]
public int Id { get; set; }
public long GlobalX { get; set; }
public long GlobalZ { get; set; }
public long PivotX { get; set; }
public long PivotZ { get; set; }
public short Size { get; set; }
public double Random { get; set; }
public int ChunkPlotType { get; set; }
public bool isCommercial { get; set; }
public bool isFarming { get; set; }
public int BiomeType { get; set; }
public short Height { get; set; }
[Indexed]
public int ChunkOwnerId { get; set; }
}
public class Heightmap {//4225 rows for every chunk
[PrimaryKey]
public int Id { get; set; }
[Indexed]
public int ChunkOwnerId { get; set; }
public float Height { get; set; }
}
public class TheMotherOfAll {
public float CoordX;
public float coordY;
public Cell[] cells;
public float[] heightmap;
}
I think the final query should look like this:
public void Load(){
List<TheMotherOfAll> theMother = new List<TheMotherOfAll>(
from rowChunks in dbManager.Table<Chunks>()
join rowCells in dbManager.Table<Cell>()
where rowChunks.X > 100 && rowChunks.X < 200//for example
select rowChunks.X as theMother.CoordX, rowChunks.Y as theMother.CoordY
on rowChunks.id equals rowCells.ChunkOwnerId
//How to continue I don't know
)
}
At the moment im working on a project where you can create a template for a report. At this point im struggling between two viable approaches for the entity model and I'm not sure which way is from the architectural approach the way to go.
A template consists of the following attributes:
Id
Name
List of Materials
List of Services
For each material and service position I have to declare a count of how many is needed.
I have two approaches how I can build it:
Approach I:
public class Template
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TemplatePosition> Positions { get; set; }
}
public class TemplatePosition
{
public int Id { get; set; }
public int Count { get; set; }
public Service Service { get; set; }
public Material Material { get; set; }
public Template Template { get; set; }
[NotMapped]
public TemplatePositionTyp Type => this.Material != null ? TemplatePositionTyp.Material : TemplatePositionTyp.Service;
}
public enum TemplatePositionTyp
{
Material = 1,
Service = 2
}
Approach II:
public class Template
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TemplateMaterialPosition> MaterialPositions { get; set; }
public ICollection<TemplateServicePosition> ServicePositions { get; set; }
}
public class TemplateMaterialPosition
{
public int Id { get; set; }
public int Count { get; set; }
public Material Material { get; set; }
public Template Template { get; set; }
}
public class TemplateServicePosition
{
public int Id { get; set; }
public int Count { get; set; }
public Service Service { get; set; }
public Template Template { get; set; }
}
I wish someone can explain me which one is the one to go and even the downside of the other approach.
I am working a Xamarin.Forms App that uses the Azure Face API. With this API you retrieve a JSON response (See Below).
I want to extract the gender of the person in the image but am having trouble with it as I am very new to this.
I extract the full JSON response into a string but I would like to be able to extract data such as the 'Gender' or the 'Age' of the person in the image.
[{"faceId":"9448dfe4-afb6-4557-94fe-010fc439ff36","faceRectangle":{"top":635,"left":639,"width":789,"height":789},"faceAttributes":{"smile":0.187,"headPose":{"pitch":0.0,"roll":-1.6,"yaw":-7.9},"gender":"male","age":34.6,"facialHair":{"moustache":0.5,"beard":0.6,"sideburns":0.6},"glasses":"NoGlasses","emotion":{"anger":0.0,"contempt":0.69,"disgust":0.0,"fear":0.0,"happiness":0.187,"neutral":0.12,"sadness":0.002,"surprise":0.0},"blur":{"blurLevel":"low","value":0.15},"exposure":{"exposureLevel":"overExposure","value":0.85},"noise":{"noiseLevel":"medium","value":0.42},"makeup":{"eyeMakeup":false,"lipMakeup":false},"accessories":[],"occlusion":{"foreheadOccluded":false,"eyeOccluded":false,"mouthOccluded":false},"hair":{"bald":0.02,"invisible":false,"hairColor":[{"color":"brown","confidence":1.0},{"color":"black","confidence":0.95},{"color":"other","confidence":0.22},{"color":"blond","confidence":0.11},{"color":"gray","confidence":0.05},{"color":"red","confidence":0.04}]}}}]
This is how I set the JSON data to a string.
string contentString = await response.Content.ReadAsStringAsync();
You usually want to create a class that represents your response e. g. Person. Then you could use the Newtonsoft.Json nuget package to deserialize the object.:
var person = JsonConvert.DeserializeObject<Person>(contentString);
Now you can access the values like:
person.Gender
Using Newtonsoft.Json:
var results = JsonConvert.DeserializeObject<AzureFace>(contentString);
Debug.WriteLine(results.faceAttributes.age);
C# Model Used (via http://jsonutils.com)
public class FaceRectangle
{
public int top { get; set; }
public int left { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class HeadPose
{
public double pitch { get; set; }
public double roll { get; set; }
public double yaw { get; set; }
}
public class FacialHair
{
public double moustache { get; set; }
public double beard { get; set; }
public double sideburns { get; set; }
}
public class Emotion
{
public double anger { get; set; }
public double contempt { get; set; }
public double disgust { get; set; }
public double fear { get; set; }
public double happiness { get; set; }
public double neutral { get; set; }
public double sadness { get; set; }
public double surprise { get; set; }
}
public class Blur
{
public string blurLevel { get; set; }
public double value { get; set; }
}
public class Exposure
{
public string exposureLevel { get; set; }
public double value { get; set; }
}
public class Noise
{
public string noiseLevel { get; set; }
public double value { get; set; }
}
public class Makeup
{
public bool eyeMakeup { get; set; }
public bool lipMakeup { get; set; }
}
public class Occlusion
{
public bool foreheadOccluded { get; set; }
public bool eyeOccluded { get; set; }
public bool mouthOccluded { get; set; }
}
public class HairColor
{
public string color { get; set; }
public double confidence { get; set; }
}
public class Hair
{
public double bald { get; set; }
public bool invisible { get; set; }
public IList<HairColor> hairColor { get; set; }
}
public class FaceAttributes
{
public double smile { get; set; }
public HeadPose headPose { get; set; }
public string gender { get; set; }
public double age { get; set; }
public FacialHair facialHair { get; set; }
public string glasses { get; set; }
public Emotion emotion { get; set; }
public Blur blur { get; set; }
public Exposure exposure { get; set; }
public Noise noise { get; set; }
public Makeup makeup { get; set; }
public IList<object> accessories { get; set; }
public Occlusion occlusion { get; set; }
public Hair hair { get; set; }
}
public class AzureFace
{
public string faceId { get; set; }
public FaceRectangle faceRectangle { get; set; }
public FaceAttributes faceAttributes { get; set; }
}
I'm trying to work with the google maps api. This URL is EXACTLY what I need:
http://maps.googleapis.com/maps/api/geocode/json?address=77379
Take a look at the results. It has all the info I need... lat, lon, state, country. Trouble is, I don't know how to extract this data. I tried this:
var client = new WebClient();
var content = client.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address=77379");
object myObject = JsonConvert.DeserializeObject(content);
While that doesn't error out, myObject doesn't end up being anything useful. (Or, maybe it is and I just don't know it?)
Here is your class structure
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Bounds
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast2 northeast { get; set; }
public Southwest2 southwest { get; set; }
}
public class Geometry
{
public Bounds bounds { get; set; }
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public string place_id { get; set; }
public List<string> postcode_localities { get; set; }
public List<string> types { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
public string status { get; set; }
}
And you have to do
RootObject rootObject = new JavaScriptSerializer().Deserialize<RootObject>(content);
Try referring to this stackoverflow posting if you would prefer other ways
Deserialize JSON with C#
From my experience using JSON I have always used this method:
object myObject = JsonConvert.DeserializeObject(content);
Would that work? Or is this something different from what you're doing?
This question already has answers here:
JSON.net: how to deserialize without using the default constructor?
(6 answers)
Closed 6 years ago.
I have a custom class that has some properties from an external dll ( i cannot change this dll class)
If property's class has an empty constructer then this property is serialized but if there is only one constructer that you have to give parameters then it cannot serialize.
For example I created an instance from this class and tried to serialize and result is "[]"
here is the class
public class Shape : ShapeBase,
{
public Shape(DocumentBase doc, ShapeType shapeType);
public Chart Chart { get; }
public bool ExtrusionEnabled { get; }
public Fill Fill { get; }
public Color FillColor { get; set; }
public bool Filled { get; set; }
public Paragraph FirstParagraph { get; }
public bool HasChart { get; }
public bool HasImage { get; }
public ImageData ImageData { get; }
public Paragraph LastParagraph { get; }
public override NodeType NodeType { get; }
public OleFormat OleFormat { get; }
public bool ShadowEnabled { get; }
public SignatureLine SignatureLine { get; }
public StoryType StoryType { get; }
public Stroke Stroke { get; }
public Color StrokeColor { get; set; }
public bool Stroked { get; set; }
public double StrokeWeight { get; set; }
public TextBox TextBox { get; }
public TextPath TextPath { get; }
public override bool Accept(DocumentVisitor visitor);
}
and i try to serialize like that
Shape shape = new Shape(_wordDocument, ShapeType.TextPlainText);
string json = JsonConvert.SerializeObject(shape, Formatting.Indented);
but other class is like
public class PdfSaveOptions : FixedPageSaveOptions
{
public PdfSaveOptions();
public int BookmarksOutlineLevel { get; set; }
public PdfCompliance Compliance { get; set; }
public bool CreateNoteHyperlinks { get; set; }
public PdfCustomPropertiesExport CustomPropertiesExport { get; set; }
public PdfDigitalSignatureDetails DigitalSignatureDetails { get; set; }
public override DmlEffectsRenderingMode DmlEffectsRenderingMode { get; set; }
public bool DownsampleImages { get; set; }
public DownsampleOptions DownsampleOptions { get; }
.......
}
it is serialized.
So I also tried to use some options like
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
but did not work.
You still need to have constructor without parameters. Otherwise Deserialize method is not able to create instance of that object. Deserialize tryies to instantiate object and then use Properties or Fields (depends on configuration) to set data to it.
You can make your constructor private or internal if you want, just so long as its parameterless. You then have kind of constructor that doesn't exists outside your class.
public class Shape : ShapeBase,
{
private Shape() {
//here some inits
}
public Shape(DocumentBase doc, ShapeType shapeType);
public Chart Chart { get; }
public bool ExtrusionEnabled { get; }
public Fill Fill { get; }
public Color FillColor { get; set; }
public bool Filled { get; set; }
public Paragraph FirstParagraph { get; }
public bool HasChart { get; }
public bool HasImage { get; }
public ImageData ImageData { get; }
public Paragraph LastParagraph { get; }
public override NodeType NodeType { get; }
public OleFormat OleFormat { get; }
public bool ShadowEnabled { get; }
public SignatureLine SignatureLine { get; }
public StoryType StoryType { get; }
public Stroke Stroke { get; }
public Color StrokeColor { get; set; }
public bool Stroked { get; set; }
public double StrokeWeight { get; set; }
public TextBox TextBox { get; }
public TextPath TextPath { get; }
public override bool Accept(DocumentVisitor visitor);
}