Skip to content

Commit bfc70e9

Browse files
authored
style: unify json initialization for rpc client and rpc server (#3948)
1 parent 22ec34a commit bfc70e9

28 files changed

+262
-297
lines changed

src/Neo.Network.RpcClient/Models/RpcAccount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class RpcAccount
2525

2626
public JObject ToJson()
2727
{
28-
return new JObject
28+
return new()
2929
{
3030
["address"] = Address,
3131
["haskey"] = HasKey,

src/Neo.Network.RpcClient/Models/RpcApplicationLog.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class RpcApplicationLog
2929

3030
public JObject ToJson()
3131
{
32-
JObject json = new JObject();
32+
var json = new JObject();
3333
if (TxId != null)
3434
json["txid"] = TxId.ToString();
3535
if (BlockHash != null)
@@ -65,14 +65,15 @@ public class Execution
6565

6666
public JObject ToJson()
6767
{
68-
JObject json = new();
69-
json["trigger"] = Trigger;
70-
json["vmstate"] = VMState;
71-
json["gasconsumed"] = GasConsumed.ToString();
72-
json["exception"] = ExceptionMessage;
73-
json["stack"] = Stack.Select(q => q.ToJson()).ToArray();
74-
json["notifications"] = Notifications.Select(q => q.ToJson()).ToArray();
75-
return json;
68+
return new()
69+
{
70+
["trigger"] = Trigger,
71+
["vmstate"] = VMState,
72+
["gasconsumed"] = GasConsumed.ToString(),
73+
["exception"] = ExceptionMessage,
74+
["stack"] = Stack.Select(q => q.ToJson()).ToArray(),
75+
["notifications"] = Notifications.Select(q => q.ToJson()).ToArray(),
76+
};
7677
}
7778

7879
public static Execution FromJson(JObject json, ProtocolSettings protocolSettings)
@@ -99,11 +100,12 @@ public class RpcNotifyEventArgs
99100

100101
public JObject ToJson()
101102
{
102-
JObject json = new();
103-
json["contract"] = Contract.ToString();
104-
json["eventname"] = EventName;
105-
json["state"] = State.ToJson();
106-
return json;
103+
return new()
104+
{
105+
["contract"] = Contract.ToString(),
106+
["eventname"] = EventName,
107+
["state"] = State.ToJson(),
108+
};
107109
}
108110

109111
public static RpcNotifyEventArgs FromJson(JObject json, ProtocolSettings protocolSettings)

src/Neo.Network.RpcClient/Models/RpcBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class RpcBlock
2424

2525
public JObject ToJson(ProtocolSettings protocolSettings)
2626
{
27-
JObject json = Utility.BlockToJson(Block, protocolSettings);
27+
var json = Utility.BlockToJson(Block, protocolSettings);
2828
json["confirmations"] = Confirmations;
2929
json["nextblockhash"] = NextBlockHash?.ToString();
3030
return json;

src/Neo.Network.RpcClient/Models/RpcBlockHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class RpcBlockHeader
2424

2525
public JObject ToJson(ProtocolSettings protocolSettings)
2626
{
27-
JObject json = Header.ToJson(protocolSettings);
27+
var json = Header.ToJson(protocolSettings);
2828
json["confirmations"] = Confirmations;
2929
json["nextblockhash"] = NextBlockHash?.ToString();
3030
return json;

src/Neo.Network.RpcClient/Models/RpcInvokeResult.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ public class RpcInvokeResult
3636

3737
public JObject ToJson()
3838
{
39-
JObject json = new();
40-
json["script"] = Script;
41-
json["state"] = State;
42-
json["gasconsumed"] = GasConsumed.ToString();
39+
var json = new JObject()
40+
{
41+
["script"] = Script,
42+
["state"] = State,
43+
["gasconsumed"] = GasConsumed.ToString()
44+
};
45+
4346
if (!string.IsNullOrEmpty(Exception))
4447
json["exception"] = Exception;
48+
4549
try
4650
{
4751
json["stack"] = new JArray(Stack.Select(p => p.ToJson()));
@@ -51,13 +55,14 @@ public JObject ToJson()
5155
// ContractParameter.ToJson() may cause InvalidOperationException
5256
json["stack"] = "error: recursive reference";
5357
}
58+
5459
if (!string.IsNullOrEmpty(Tx)) json["tx"] = Tx;
5560
return json;
5661
}
5762

5863
public static RpcInvokeResult FromJson(JObject json)
5964
{
60-
RpcInvokeResult invokeScriptResult = new()
65+
var invokeScriptResult = new RpcInvokeResult()
6166
{
6267
Script = json["script"].AsString(),
6368
State = json["state"].GetEnum<VMState>(),
@@ -81,13 +86,7 @@ public class RpcStack
8186

8287
public JToken Value { get; set; }
8388

84-
public JObject ToJson()
85-
{
86-
JObject json = new();
87-
json["type"] = Type;
88-
json["value"] = Value;
89-
return json;
90-
}
89+
public JObject ToJson() => new() { ["type"] = Type, ["value"] = Value };
9190

9291
public static RpcStack FromJson(JObject json)
9392
{

src/Neo.Network.RpcClient/Models/RpcNep17Balances.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ public class RpcNep17Balances
2525

2626
public JObject ToJson(ProtocolSettings protocolSettings)
2727
{
28-
JObject json = new();
29-
json["balance"] = Balances.Select(p => p.ToJson()).ToArray();
30-
json["address"] = UserScriptHash.ToAddress(protocolSettings.AddressVersion);
31-
return json;
28+
return new()
29+
{
30+
["balance"] = Balances.Select(p => p.ToJson()).ToArray(),
31+
["address"] = UserScriptHash.ToAddress(protocolSettings.AddressVersion)
32+
};
3233
}
3334

3435
public static RpcNep17Balances FromJson(JObject json, ProtocolSettings protocolSettings)
3536
{
36-
RpcNep17Balances nep17Balance = new()
37+
return new()
3738
{
3839
Balances = ((JArray)json["balance"]).Select(p => RpcNep17Balance.FromJson((JObject)p, protocolSettings)).ToList(),
3940
UserScriptHash = json["address"].ToScriptHash(protocolSettings)
4041
};
41-
return nep17Balance;
4242
}
4343
}
4444

@@ -52,22 +52,22 @@ public class RpcNep17Balance
5252

5353
public JObject ToJson()
5454
{
55-
JObject json = new();
56-
json["assethash"] = AssetHash.ToString();
57-
json["amount"] = Amount.ToString();
58-
json["lastupdatedblock"] = LastUpdatedBlock;
59-
return json;
55+
return new()
56+
{
57+
["assethash"] = AssetHash.ToString(),
58+
["amount"] = Amount.ToString(),
59+
["lastupdatedblock"] = LastUpdatedBlock
60+
};
6061
}
6162

6263
public static RpcNep17Balance FromJson(JObject json, ProtocolSettings protocolSettings)
6364
{
64-
RpcNep17Balance balance = new()
65+
return new()
6566
{
6667
AssetHash = json["assethash"].ToScriptHash(protocolSettings),
6768
Amount = BigInteger.Parse(json["amount"].AsString()),
6869
LastUpdatedBlock = (uint)json["lastupdatedblock"].AsNumber()
6970
};
70-
return balance;
7171
}
7272
}
7373
}

src/Neo.Network.RpcClient/Models/RpcNep17Transfers.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ public class RpcNep17Transfers
2727

2828
public JObject ToJson(ProtocolSettings protocolSettings)
2929
{
30-
JObject json = new();
31-
json["sent"] = Sent.Select(p => p.ToJson(protocolSettings)).ToArray();
32-
json["received"] = Received.Select(p => p.ToJson(protocolSettings)).ToArray();
33-
json["address"] = UserScriptHash.ToAddress(protocolSettings.AddressVersion);
34-
return json;
30+
return new()
31+
{
32+
["sent"] = Sent.Select(p => p.ToJson(protocolSettings)).ToArray(),
33+
["received"] = Received.Select(p => p.ToJson(protocolSettings)).ToArray(),
34+
["address"] = UserScriptHash.ToAddress(protocolSettings.AddressVersion)
35+
};
3536
}
3637

3738
public static RpcNep17Transfers FromJson(JObject json, ProtocolSettings protocolSettings)
3839
{
39-
RpcNep17Transfers transfers = new()
40+
return new()
4041
{
4142
Sent = ((JArray)json["sent"]).Select(p => RpcNep17Transfer.FromJson((JObject)p, protocolSettings)).ToList(),
4243
Received = ((JArray)json["received"]).Select(p => RpcNep17Transfer.FromJson((JObject)p, protocolSettings)).ToList(),
4344
UserScriptHash = json["address"].ToScriptHash(protocolSettings)
4445
};
45-
return transfers;
4646
}
4747
}
4848

@@ -64,15 +64,16 @@ public class RpcNep17Transfer
6464

6565
public JObject ToJson(ProtocolSettings protocolSettings)
6666
{
67-
JObject json = new();
68-
json["timestamp"] = TimestampMS;
69-
json["assethash"] = AssetHash.ToString();
70-
json["transferaddress"] = UserScriptHash?.ToAddress(protocolSettings.AddressVersion);
71-
json["amount"] = Amount.ToString();
72-
json["blockindex"] = BlockIndex;
73-
json["transfernotifyindex"] = TransferNotifyIndex;
74-
json["txhash"] = TxHash.ToString();
75-
return json;
67+
return new()
68+
{
69+
["timestamp"] = TimestampMS,
70+
["assethash"] = AssetHash.ToString(),
71+
["transferaddress"] = UserScriptHash?.ToAddress(protocolSettings.AddressVersion),
72+
["amount"] = Amount.ToString(),
73+
["blockindex"] = BlockIndex,
74+
["transfernotifyindex"] = TransferNotifyIndex,
75+
["txhash"] = TxHash.ToString()
76+
};
7677
}
7778

7879
public static RpcNep17Transfer FromJson(JObject json, ProtocolSettings protocolSettings)

src/Neo.Network.RpcClient/Models/RpcPeers.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public class RpcPeers
2424

2525
public JObject ToJson()
2626
{
27-
JObject json = new();
28-
json["unconnected"] = new JArray(Unconnected.Select(p => p.ToJson()));
29-
json["bad"] = new JArray(Bad.Select(p => p.ToJson()));
30-
json["connected"] = new JArray(Connected.Select(p => p.ToJson()));
31-
return json;
27+
return new()
28+
{
29+
["unconnected"] = new JArray(Unconnected.Select(p => p.ToJson())),
30+
["bad"] = new JArray(Bad.Select(p => p.ToJson())),
31+
["connected"] = new JArray(Connected.Select(p => p.ToJson()))
32+
};
3233
}
3334

3435
public static RpcPeers FromJson(JObject json)
@@ -48,13 +49,7 @@ public class RpcPeer
4849

4950
public int Port { get; set; }
5051

51-
public JObject ToJson()
52-
{
53-
JObject json = new();
54-
json["address"] = Address;
55-
json["port"] = Port;
56-
return json;
57-
}
52+
public JObject ToJson() => new() { ["address"] = Address, ["port"] = Port };
5853

5954
public static RpcPeer FromJson(JObject json)
6055
{

src/Neo.Network.RpcClient/Models/RpcPlugin.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public class RpcPlugin
2424

2525
public JObject ToJson()
2626
{
27-
JObject json = new();
28-
json["name"] = Name;
29-
json["version"] = Version;
30-
json["interfaces"] = new JArray(Interfaces.Select(p => (JToken)p));
31-
return json;
27+
return new()
28+
{
29+
["name"] = Name,
30+
["version"] = Version,
31+
["interfaces"] = new JArray(Interfaces.Select(p => (JToken)p))
32+
};
3233
}
3334

3435
public static RpcPlugin FromJson(JObject json)

src/Neo.Network.RpcClient/Models/RpcRawMemPool.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public class RpcRawMemPool
2525

2626
public JObject ToJson()
2727
{
28-
JObject json = new();
29-
json["height"] = Height;
30-
json["verified"] = new JArray(Verified.Select(p => (JToken)p.ToString()));
31-
json["unverified"] = new JArray(UnVerified.Select(p => (JToken)p.ToString()));
32-
return json;
28+
return new()
29+
{
30+
["height"] = Height,
31+
["verified"] = new JArray(Verified.Select(p => (JToken)p.ToString())),
32+
["unverified"] = new JArray(UnVerified.Select(p => (JToken)p.ToString()))
33+
};
3334
}
3435

3536
public static RpcRawMemPool FromJson(JObject json)

0 commit comments

Comments
 (0)