Skip to content

Commit

Permalink
feat(Json): MultiAddress, MultiHash and Cid are simple strings in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Oct 28, 2018
1 parent e67098a commit f261642
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Cid.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Google.Protobuf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Text;

namespace Ipfs
Expand All @@ -22,6 +24,7 @@ namespace Ipfs
/// </note>
/// </remarks>
/// <seealso href="https://github.com/ipld/cid"/>
[JsonConverter(typeof(Cid.Json))]
public class Cid : IEquatable<Cid>
{
/// <summary>
Expand Down Expand Up @@ -482,5 +485,33 @@ static public implicit operator string(Cid id)
{
return id.Encode();
}

/// <summary>
/// Conversion of a <see cref="Cid"/> to and from JSON.
/// </summary>
/// <remarks>
/// The JSON is just a single string value.
/// </remarks>
class Json : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override bool CanRead => true;
public override bool CanWrite => true;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var cid = value as Cid;
writer.WriteValue(cid?.Encode());
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
return s == null ? null : Cid.Decode(s);
}
}

}
}
30 changes: 30 additions & 0 deletions src/MultiAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using Common.Logging;
using Google.Protobuf;
using Newtonsoft.Json;

namespace Ipfs
{
Expand All @@ -25,6 +26,7 @@ namespace Ipfs
/// </para>
/// </remarks>
/// <seealso href="https://github.com/multiformats/multiaddr"/>
[JsonConverter(typeof(MultiAddress.Json))]
public class MultiAddress : IEquatable<MultiAddress>
{
/// <summary>
Expand Down Expand Up @@ -379,6 +381,34 @@ public static MultiAddress TryCreate(string s)
}
}

/// <summary>
/// Conversion of a <see cref="MultiAddress"/> to and from JSON.
/// </summary>
/// <remarks>
/// The JSON is just a single string value.
/// </remarks>
class Json : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override bool CanRead => true;
public override bool CanWrite => true;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var ma = value as MultiAddress;
writer.WriteValue(ma?.ToString());
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
return s == null ? null : new MultiAddress(s);
}
}


}

}
29 changes: 29 additions & 0 deletions src/MultiHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Common.Logging;
using Google.Protobuf;
using Ipfs.Registry;
using Newtonsoft.Json;

namespace Ipfs
{
Expand All @@ -18,6 +19,7 @@ namespace Ipfs
/// See the <see cref="HashingAlgorithm">registry</see> for supported algorithms.
/// </remarks>
/// <seealso href="https://github.com/jbenet/multihash"/>
[JsonConverter(typeof(MultiHash.Json))]
public class MultiHash : IEquatable<MultiHash>
{
static readonly ILog log = LogManager.GetLogger<MultiHash>();
Expand Down Expand Up @@ -497,6 +499,32 @@ public static MultiHash ComputeHash(Stream data, string algorithmName = DefaultA
return new MultiHash(algorithmName, GetHashAlgorithm(algorithmName).ComputeHash(data));
}

/// <summary>
/// Conversion of a <see cref="MultiHash"/> to and from JSON.
/// </summary>
/// <remarks>
/// The JSON is just a single string value.
/// </remarks>
class Json : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override bool CanRead => true;
public override bool CanWrite => true;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var mh = value as MultiHash;
writer.WriteValue(mh?.ToString());
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
return s == null ? null : new MultiHash(s);
}
}
}

/// <summary>
Expand All @@ -510,4 +538,5 @@ public class UnknownHashingAlgorithmEventArgs : EventArgs
/// </summary>
public HashingAlgorithm Algorithm { get; set; }
}

}
34 changes: 34 additions & 0 deletions test/CidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using Google.Protobuf;
using Newtonsoft.Json;

namespace Ipfs
{
Expand Down Expand Up @@ -359,5 +360,38 @@ public void Immutable()
ExceptionAssert.Throws<NotSupportedException>(() => cid.Hash = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L5");
ExceptionAssert.Throws<NotSupportedException>(() => cid.Version = 0);
}

class CidAndX
{
public Cid Cid;
public int X;
}

[TestMethod]
public void JsonSerialization()
{
Cid a = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4";
string json = JsonConvert.SerializeObject(a);
Assert.AreEqual($"\"{a.Encode()}\"", json);
var b = JsonConvert.DeserializeObject<Cid>(json);
Assert.AreEqual(a, b);

a = null;
json = JsonConvert.SerializeObject(a);
b = JsonConvert.DeserializeObject<Cid>(json);
Assert.IsNull(b);

var x = new CidAndX { Cid = "QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4", X = 42 };
json = JsonConvert.SerializeObject(x);
var y = JsonConvert.DeserializeObject<CidAndX>(json);
Assert.AreEqual(x.Cid, y.Cid);
Assert.AreEqual(x.X, y.X);

x.Cid = null;
json = JsonConvert.SerializeObject(x);
y = JsonConvert.DeserializeObject<CidAndX>(json);
Assert.AreEqual(x.Cid, y.Cid);
Assert.AreEqual(x.X, y.X);
}
}
}
17 changes: 17 additions & 0 deletions test/MultiAddressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using Newtonsoft.Json;

namespace Ipfs
{
Expand Down Expand Up @@ -307,6 +308,22 @@ public void TryCreate()
Assert.IsNull(MultiAddress.TryCreate("/tcp/alpha")); // bad port
Assert.IsNull(MultiAddress.TryCreate("/foobar")); // bad protocol
}

[TestMethod]
public void JsonSerialization()
{
var a = new MultiAddress("/ip6/fe80::7573:b0a8:46b0:0bad/tcp/4009");
string json = JsonConvert.SerializeObject(a);
Assert.AreEqual($"\"{a.ToString()}\"", json);
var b = JsonConvert.DeserializeObject<MultiAddress>(json);
Assert.AreEqual(a.ToString(), b.ToString());

a = null;
json = JsonConvert.SerializeObject(a);
b = JsonConvert.DeserializeObject<MultiAddress>(json);
Assert.IsNull(b);
}

}
}

16 changes: 16 additions & 0 deletions test/MultiHashTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.IO;
using Google.Protobuf;
using Newtonsoft.Json;

namespace Ipfs
{
Expand Down Expand Up @@ -469,5 +470,20 @@ public void Binary()
CollectionAssert.AreEqual(mh.Digest, mh1.Digest);
}

[TestMethod]
public void JsonSerialization()
{
var a = new MultiHash("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
string json = JsonConvert.SerializeObject(a);
Assert.AreEqual($"\"{a.ToString()}\"", json);
var b = JsonConvert.DeserializeObject<MultiHash>(json);
Assert.AreEqual(a, b);

a = null;
json = JsonConvert.SerializeObject(a);
b = JsonConvert.DeserializeObject<MultiHash>(json);
Assert.IsNull(b);
}

}
}

0 comments on commit f261642

Please sign in to comment.