generated from bmresearch/solnet-programs-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed accounts parsing when there is extra metadata (need to handle d…
…ocs later) (#20)
- Loading branch information
Showing
3 changed files
with
103 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
|
||
} | ||
} |