Skip to content

Commit

Permalink
Fixed accounts parsing when there is extra metadata (need to handle d…
Browse files Browse the repository at this point in the history
…ocs later) (#20)
  • Loading branch information
tiago18c authored Jun 29, 2022
1 parent 558d103 commit ffa353a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 119 deletions.
2 changes: 1 addition & 1 deletion SharedBuildProperties.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Solnet</Product>
<Version>0.2.7</Version>
<Version>0.2.8</Version>
<Copyright>Copyright 2022 &#169; Solnet</Copyright>
<Authors>blockmountain</Authors>
<PublisherName>blockmountain</PublisherName>
Expand Down
174 changes: 78 additions & 96 deletions Solnet.Anchor/Converters/IIdlAccountItemConverter.cs
Original file line number Diff line number Diff line change
@@ -1,97 +1,79 @@
using Solnet.Anchor.Models.Accounts;
using Solnet.Anchor.Models.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Solnet.Anchor.Converters
{
public class IIdlAccountItemConverter : JsonConverter<IIdlAccountItem[]>
{
public override IIdlAccountItem[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray) return null;

List<IIdlAccountItem> accountItems = new List<IIdlAccountItem>();

while (reader.Read() && reader.TokenType == JsonTokenType.StartObject)
{
//IIdlAccountItem acc =

reader.Read();
if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value.");

string propertyName = reader.GetString();
if ("name" != propertyName) throw new JsonException("Unexpected error value.");

reader.Read();
if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value.");

string name = reader.GetString();

reader.Read();
if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value.");

propertyName = reader.GetString();
reader.Read();

if ("accounts" == propertyName)
{
IdlAccounts accounts = new();
accounts.Name = name;

accounts.Accounts = Read(ref reader, typeToConvert, options);
accountItems.Add(accounts);
}
else
{
IdlAccount account = new();
account.Name = name;

if ("isMut" != propertyName) throw new JsonException("Unexpected error value.");
account.IsMut = reader.GetBoolean();

reader.Read();

if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value.");

propertyName = reader.GetString();
reader.Read();
if ("isSigner" != propertyName) throw new JsonException("Unexpected error value.");
account.IsSigner = reader.GetBoolean();
accountItems.Add(account);

reader.Read();

if(reader.TokenType == JsonTokenType.PropertyName)
{
var prop = reader.GetString();

if ("pda" != prop) throw new JsonException("Unexpected property.");

reader.Read();

account.Pda = JsonSerializer.Deserialize<IdlPda>(ref reader, options);
reader.Read();
}
}

// object end
if(reader.TokenType != JsonTokenType.EndObject)
reader.Read();
}
//array end
//reader.Read();
return accountItems.ToArray();
}

public override void Write(Utf8JsonWriter writer, IIdlAccountItem[] value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
using Solnet.Anchor.Models.Accounts;
using Solnet.Anchor.Models.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Solnet.Anchor.Converters
{
public class IIdlAccountItemConverter : JsonConverter<IIdlAccountItem[]>
{
public override IIdlAccountItem[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray) return null;

List<IIdlAccountItem> accountItems = new List<IIdlAccountItem>();

reader.Read();


while (reader.TokenType == JsonTokenType.StartObject)
{
Utf8JsonReader readerCopy = reader;
//IIdlAccountItem acc =

reader.Read();
if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value.");

string propertyName = reader.GetString();
if ("name" != propertyName) throw new JsonException("Unexpected error value.");

reader.Read();
if (reader.TokenType != JsonTokenType.String) throw new JsonException("Unexpected error value.");

string name = reader.GetString();

reader.Read();
if (reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Unexpected error value.");

propertyName = reader.GetString();
reader.Read();

if ("accounts" == propertyName)
{
IdlAccounts accounts = new();
accounts.Name = name;

accounts.Accounts = Read(ref reader, typeToConvert, options);
accountItems.Add(accounts);
}
else
{
IdlAccount account = JsonSerializer.Deserialize<IdlAccount>(ref readerCopy, options);

accountItems.Add(account);

reader = readerCopy;
}

// object end
if(reader.TokenType != JsonTokenType.EndObject)
reader.Read();
reader.Read();

}
//array end
//reader.Read();
return accountItems.ToArray();
}

public override void Write(Utf8JsonWriter writer, IIdlAccountItem[] value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
46 changes: 24 additions & 22 deletions Solnet.Anchor/Models/Accounts/IdlAccount.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
using Solnet.Anchor.CodeGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Solnet.Anchor.Models.Accounts
{
public class IdlAccount : IIdlAccountItem
{
public string Name { get; set; }

public string NamePascalCase => Name.ToPascalCase();

public bool IsMut { get; set; }

public bool IsSigner { get; set; }

public IdlPda Pda { get; set; }

}
using Solnet.Anchor.CodeGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Solnet.Anchor.Models.Accounts
{
public class IdlAccount : IIdlAccountItem
{
public string Name { get; set; }

public string Description { get; set; }

public string NamePascalCase => Name.ToPascalCase();

public bool IsMut { get; set; }

public bool IsSigner { get; set; }

public IdlPda Pda { get; set; }

}
}

0 comments on commit ffa353a

Please sign in to comment.