Skip to content

Commit

Permalink
Merge pull request #706 from aydjay/jsonNet
Browse files Browse the repository at this point in the history
Removed Old Json Data Contract Serialiser in favour of using Json.Net
  • Loading branch information
Stickymaddness committed Aug 3, 2017
2 parents 2a1c114 + 61c7803 commit 8432884
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 151 deletions.
2 changes: 1 addition & 1 deletion POEApi.Model/Gem.cs
Expand Up @@ -18,7 +18,7 @@ public Gem(JSONProxy.Item item) : base(item)
this.ItemType = Model.ItemType.Gem;

this.Socket = item.Socket;
this.Color = item.Color;
this.Color = item.Colour;
this.Requirements = ProxyMapper.GetRequirements(item.Requirements);
this.Level = getLevel();
}
Expand Down
12 changes: 6 additions & 6 deletions POEApi.Model/Item.cs
Expand Up @@ -74,7 +74,7 @@ protected Item(JSONProxy.Item item)
this.ItemType = Model.ItemType.UnSet;
this.CraftedMods = item.CraftedMods;
this.EnchantMods = item.EnchantMods;
this.ItemLevel = item.ItemLevel;
this.ItemLevel = item.Ilvl;

if (item.Properties != null)
{
Expand All @@ -88,8 +88,8 @@ protected Item(JSONProxy.Item item)
}

this.Corrupted = item.Corrupted;
this.Microtransactions = item.CosmeticMods == null ? new List<string>() : item.CosmeticMods;
this.EnchantMods = item.EnchantMods == null ? new List<string>() : item.EnchantMods;
this.Microtransactions = item.CosmeticMods ?? new List<string>();
this.EnchantMods = item.EnchantMods ?? new List<string>();

this.TradeX = this.X;
this.TradeY = this.Y;
Expand Down Expand Up @@ -128,11 +128,11 @@ protected int getHash()
protected Rarity getRarity(JSONProxy.Item item)
{
//Looks like isRelic is coming across the wire as an additional field but coincidentally 9 was the correct frame type here.
if (item.frameType == 9 || item.IsRelic)
if (item.FrameType == 9 || item.IsRelic)
return Rarity.Relic;

if (item.frameType <= 3)
return (Rarity)item.frameType;
if (item.FrameType <= 3)
return (Rarity)item.FrameType;

return Rarity.Normal;
}
Expand Down
6 changes: 3 additions & 3 deletions POEApi.Model/ItemFactory.cs
Expand Up @@ -16,7 +16,7 @@ public static Item Get(JSONProxy.Item item)
if(!string.IsNullOrWhiteSpace(item.ProphecyText))
return new Prophecy(item);

if (item.frameType == 4)
if (item.FrameType == 4)
return new Gem(item);

if (item.DescrText != null && item.DescrText.ToLower() == "right click this item then left click a location on the ground to create the object.")
Expand All @@ -25,7 +25,7 @@ public static Item Get(JSONProxy.Item item)
if (item.TypeLine.Contains("Leaguestone"))
return new Leaguestone(item);

if (item.frameType == 5)
if (item.FrameType == 5)
return GetCurrency(item);

if (item.TypeLine.Contains("Map") && item.DescrText != null && item.DescrText.Contains("Travel to this Map"))
Expand All @@ -38,7 +38,7 @@ public static Item Get(JSONProxy.Item item)
Logger.Log(ex);
var errorMessage = "ItemFactory unable to instantiate type : " + item.TypeLine;
Logger.Log(errorMessage);
throw new Exception(errorMessage);
throw;
}
}

Expand Down
134 changes: 16 additions & 118 deletions POEApi.Model/JSONProxy/Stash.cs
@@ -1,187 +1,87 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace POEApi.Model.JSONProxy
{
[DataContract]
public class Property
{
[DataMember(Name = "name")]
public string Name { get; set; }

[DataMember(Name = "values")]
public List<object> Values { get; set; }

[DataMember(Name = "displayMode")]
public int DisplayMode { get; set; }
}

[DataContract]
public class AdditionalProperty
{
[DataMember(Name = "name")]
public string name { get; set; }

[DataMember(Name = "values")]
public List<List<object>> values { get; set; }

[DataMember(Name = "displayMode")]
public int displayMode { get; set; }

[DataMember(Name = "progress")]
public double progress { get; set; }
public string Name { get; set; }
public List<List<object>> Values { get; set; }
public int DisplayMode { get; set; }
public double Progress { get; set; }
}


[DataContract]
public class Requirement
{
[DataMember(Name = "name")]
public string Name { get; set; }

[DataMember(Name = "values")]
public List<object> Value { get; set; }

[DataMember(Name = "displayMode")]
public List<object> Values { get; set; }
public int DisplayMode { get; set; }
}

[DataContract]
public class Item
{
[DataMember(Name = "id")]
public string Id { get; set; }

[DataMember(Name = "verified")]
public bool Verified { get; set; }

[DataMember(Name = "w")]
public int W { get; set; }

[DataMember(Name = "h")]
public int H { get; set; }

[DataMember(Name = "icon")]
public string Icon { get; set; }

[DataMember(Name = "support")]
public bool Support { get; set; }

[DataMember(Name = "league")]
public string League { get; set; }

[DataMember(Name = "name")]
public string Name { get; set; }

[DataMember(Name = "typeLine")]
public string TypeLine { get; set; }

[DataMember(Name = "identified")]
public bool Identified { get; set; }

[DataMember(Name = "properties")]
public List<Property> Properties { get; set; }

[DataMember(Name = "explicitMods")]
public List<string> ExplicitMods { get; set; }

[DataMember(Name = "descrText")]
public string DescrText { get; set; }

[DataMember(Name = "frameType")]
public int frameType { get; set; }

[DataMember(Name = "x")]
public int FrameType { get; set; }
public int X { get; set; }

[DataMember(Name = "y")]
public int Y { get; set; }

[DataMember(Name = "inventoryId")]
public string InventoryId { get; set; }

[DataMember(Name = "socketedItems")]
public List<Item> SocketedItems { get; set; }

[DataMember(Name = "sockets")]
public List<Socket> Sockets { get; set; }

[DataMember(Name = "additionalProperties")]
public List<AdditionalProperty> additionalProperties { get; set; }

[DataMember(Name = "secDescrText")]
public List<AdditionalProperty> AdditionalProperties { get; set; }
public string SecDescrText { get; set; }

[DataMember(Name = "implicitMods")]
public List<string> ImplicitMods { get; set; }

[DataMember(Name = "flavourText")]
public List<string> FlavourText { get; set; }

[DataMember(Name = "requirements")]
public List<Requirement> Requirements { get; set; }

[DataMember(Name = "nextLevelRequirements")]
public List<Requirement> nextLevelRequirements { get; set; }

[DataMember(Name = "socket")]
public List<Requirement> NextLevelRequirements { get; set; }
public int Socket { get; set; }

[DataMember(Name = "colour")]
public string Color { get; set; }

[DataMember(Name = "corrupted")]
public string Colour { get; set; }
public bool Corrupted { get; set; }

[DataMember(Name = "cosmeticMods")]
public List<string> CosmeticMods { get; set; }

[DataMember(Name = "craftedMods")]
public List<string> CraftedMods { get; set; }

[DataMember(Name = "enchantMods")]
public List<string> EnchantMods { get; set; }

[DataMember(Name= "ilvl")]
public int ItemLevel { get; set; }

[DataMember(Name = "prophecyText")]
public int Ilvl { get; set; }
public string ProphecyText { get; set; }

[DataMember(Name = "prophecyDiffText")]
public string ProphecyDifficultyText { get; set; }

[DataMember(Name = "isRelic")]
public string ProphecyDiffText { get; set; }
public bool IsRelic { get; set; }
}

[DataContract]
public class Socket
{
[DataMember(Name = "attr")]
public string Attribute { get; set; }

[DataMember(Name = "group")]
public string Attr { get; set; }
public int Group { get; set; }
}

[DataContract(Name = "RootObject")]
[JsonObject(MemberSerialization.OptOut)]
public class Stash
{
[DataMember(Name = "numTabs")]
public int NumTabs { get; set; }

[DataMember(Name = "items")]
public List<Item> Items { get; set; }

[DataMember(Name = "tabs")]
public List<Tab> Tabs { get; set; }
}

[DataContract(Name = "RootObject")]
[JsonObject(MemberSerialization.OptOut)]
public class Inventory
{
[DataMember(Name = "items")]
public List<Item> Items { get; set; }
}

Expand All @@ -204,6 +104,4 @@ public class Tab
public string type { get; set; }
public bool selected { get; set; }
}
}


}
1 change: 0 additions & 1 deletion POEApi.Model/POEApi.Model.csproj
Expand Up @@ -40,7 +40,6 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
26 changes: 10 additions & 16 deletions POEApi.Model/POEModel.cs
@@ -1,16 +1,15 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using POEApi.Infrastructure;
using POEApi.Model.Events;
using POEApi.Transport;
using System.Security;
using System;
using System.Diagnostics;
using POEApi.Infrastructure.Events;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using POEApi.Model.JSONProxy;

namespace POEApi.Model
Expand Down Expand Up @@ -102,7 +101,7 @@ private T GetProperObjectFromTransport<T>(Stream networkData)
{
try
{
var serializer = new JsonSerializer();
var serializer = new JsonSerializer {ContractResolver = new CamelCasePropertyNamesContractResolver()};
return (T) serializer.Deserialize(jsonTextReader, typeof (T));
}
catch
Expand Down Expand Up @@ -136,18 +135,17 @@ public void ForceRefresh()
cacheService.Clear();
}

public Stash GetStash(int index, string league, string accountName, bool forceRefresh = false)
{
DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(JSONProxy.Stash));
public Stash GetStash(int index, string league, string accountName, bool forceRefresh = false)
{
JSONProxy.Stash proxy = null;

onStashLoaded(POEEventState.BeforeEvent, index, -1);

using (Stream stream = Transport.GetStash(index, league, accountName, forceRefresh))
using (var stream = Transport.GetStash(index, league, accountName, forceRefresh))
{
try
{
proxy = (JSONProxy.Stash)serialiser.ReadObject(stream);
try
{
proxy = GetProperObjectFromTransport<JSONProxy.Stash>(stream);
if (proxy == null)
logNullStash(stream, "Proxy was null");
}
Expand Down Expand Up @@ -268,18 +266,14 @@ public List<Item> GetInventory(string characterName, bool forceRefresh, string a
if (downOnlyMyCharacters && !Settings.Lists["MyCharacters"].Contains(characterName))
return new List<Item>();

DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(JSONProxy.Inventory));
JSONProxy.Inventory item;

using (Stream stream = Transport.GetInventory(characterName, forceRefresh, accountName))
item = (JSONProxy.Inventory)serialiser.ReadObject(stream);
Inventory item = GetProperObjectFromTransport<Inventory>(Transport.GetInventory(characterName, forceRefresh, accountName));

if (item.Items == null)
return new List<Item>();

return item.Items.Select(i => ItemFactory.Get(i)).ToList();
}
catch (SerializationException sex)
catch (Exception sex)
{
Logger.Log(string.Format("Error reading character data for character '{0}', Exception info: ", characterName, sex.ToString()));
throw new Exception(string.Format("Error reading character data for {0}, if you are in offline mode you will need to login and update. If you received this error while logging in, the authenticated session may have expired or bad data has been returned by GGG or a network issue may have occurred - Please try again.", characterName));
Expand Down
3 changes: 2 additions & 1 deletion POEApi.Model/Property.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace POEApi.Model
{
Expand All @@ -16,7 +17,7 @@ internal Property(JSONProxy.Property property)

foreach (object value in property.Values)
{
object[] pair = (object[])value;
var pair = (JArray) value;
Values.Add(new Tuple<string, int>(pair[0].ToString(), int.Parse(pair[1].ToString())));
}

Expand Down
2 changes: 1 addition & 1 deletion POEApi.Model/Prophecy.cs
Expand Up @@ -12,7 +12,7 @@ internal Prophecy(JSONProxy.Item item) : base(item)
{
this.ProphecyText = item.ProphecyText;
this.FlavourText = item.FlavourText;
this.ProphecyDifficultyText = item.ProphecyDifficultyText;
this.ProphecyDifficultyText = item.ProphecyDiffText;
}

protected override int getConcreteHash()
Expand Down

0 comments on commit 8432884

Please sign in to comment.