7
votes

I have a class as so

[Serializable]
public class ExternalAccount
{
  public string Name { get;set;}      
}

I have converted this to JSON like so

{\"Name\":\"XYZ\"}

I have then base64 encoded the JSON string

I then send across the wire to a web api service

I receive the base64 encoded string and now need to de-serialize it back to the original object as above (ExternalAccount) so i firstly do a

byte[] byteArray = Convert.FromBase64String(base64EncodedExternalAccount);

What is the next step?

I have tried the below but this returns null...

using (MemoryStream memoryStream = new MemoryStream(byteArrayToConvert))
 {
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            // set memory stream position to starting point
            memoryStream.Position = 0;

            // Deserializes a stream into an object graph and return as a               object.
            return binaryFormatter.Deserialize(memoryStream) as ExternalAccount;
  }

Any pointers/tips greatly appreciated.

2
BinaryFormatter cannot deserialize objects from JSON strings. It is .NET specific binary serialization format. Can you show full example of how you encode JSON string to BASE64?hal
Do you really need to post to your API as a base-64 string? If possible then update your API so that you can send plain JSON instead.LukeH
thanks Hal - that is good to know for the future.AdrianSean
@LukeH - yes i need to base64 encode the string as i have omitted a large amount of properties from the code example above and i want to ensure the object isnt accidentally messed with in transit across the wireAdrianSean

2 Answers

21
votes

You can try converting the byte array back to string (it will be the same JSON you sent), then deserialize to the ExternalAccount object. Using the Newtonsoft JSON library the following sample correctly displays "Someone" on the console:

class Program
{
    static void Main(string[] args)
    {
        var account = new ExternalAccount() { Name = "Someone" };
        string json = JsonConvert.SerializeObject(account);
        string base64EncodedExternalAccount = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
        byte[] byteArray = Convert.FromBase64String(base64EncodedExternalAccount);

        string jsonBack = Encoding.UTF8.GetString(byteArray);
        var accountBack = JsonConvert.DeserializeObject<ExternalAccount>(jsonBack);
        Console.WriteLine(accountBack.Name);
        Console.ReadLine();
    }
}

[Serializable]
public class ExternalAccount
{
    public string Name { get; set; }
}
3
votes

you need to extract string from the bytes you recieve.

byte[] byteArray = Convert.FromBase64String(base64EncodedExternalAccount);
string AccountInfo = System.Text.Encoding.UTF8.GetString(byteArray );

As expected, you will get {\"Name\":\"XYZ\"} in your AccountInfo string. Now you need to Deserialize. you can use the same model, ExternalAccount. you may do something like:

ExnternalAccount model = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<ExnternalAccount>(AccountInfo );