1

Please notice that the object is defined in proto file, not in a common cs file. It has different behavior.

I'm using c#. Part of my proto file is:

    message CodeDependency {
        string path = 1;
        DependencyType type = 2;

        enum DependencyType {
            NONE = 0;
            TAR = 1;
            ZIP = 2;
            TAR_GZ = 3;
            DIRECTORY = 4;
        }
    }

And I have a json string:

{"codeDependency": {
                    "path": "/CAP_TEST/job_manager/modules/1c8185d5-2add-4bd4-a332-8b21a6819608/tmpr9z7xinh.tar.gz",
                    "type": "TAR_GZ"
                }}

I've tried three ways to deserialize it:

Newtonsoft.Json.JsonConvert.DeserializeObject<CodeDependency>
CodeDependency.Parser.ParseFrom
ProtoBuf.Serializer.Deserialize<CodeDependency>

None of them works. From the error message, it seems that 'TAR_GZ' can't be deserialized.

Error converting value "TAR_GZ" to type 'Microsoft.ABC.GRPC.Modules.Module+Types+CodeDependency+Types+DependencyType'. Path 'graph.nodes[4].module.codeDependency.type', line 273, position 21. ---> System.ArgumentException: Requested value 'TAR_GZ' was not found

If I change 'TAR_GZ' to 'TAR', it's ok. So maybe the problem is related to the underline in 'TAR_GZ'? Is there any way to solve it in C#? (It's ok in python.) Thanks for your help!

  • Try NewtonsoftJson: https://www.nuget.org/packages/Newtonsoft.Json/ – Felix Arnold Nov 29 '19 at 10:08
  • @MaxMustermann he already has – Innat3 Nov 29 '19 at 10:09
  • Does this answer your question? [Can't get enum to convert to json properly using Json.NET](https://stackoverflow.com/questions/19767863/cant-get-enum-to-convert-to-json-properly-using-json-net) – Drag and Drop Nov 29 '19 at 10:10
  • What do you get if you *serialize* the protobuf message that contains the data you want? (To JSON) – Jon Skeet Nov 29 '19 at 10:23
  • Thanks for your comments. I've tried (Newtonsoft.Json)JsonConvert.DeserializeObject. The problem is: For class(and enum) defined in common cs file, it works fine. But for class defined in *.proto(and compiled to cs by protobuf dll), it doesn't work. The error message is "Error converting value "TAR_GZ" to type 'Microsoft.ABC.GRPC.Modules.Module+Types+CodeDependency+Types+DependencyType'. Path 'graph.nodes[4].module.codeDependency.type', line 273, position 21. ---> System.ArgumentException: Requested value 'TAR_GZ' was not found" – user9345277 Dec 02 '19 at 02:13

1 Answers1

0

I´ve tried your problem with NewtonsoftJson and an Root-Object, that worked for me here my solution:

    namespace RegexTests
{
    static class StringExtension
    {
        public static T DeserializeJson<T>(this string toSerialize)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(toSerialize, new Newtonsoft.Json.JsonSerializerSettings()
            {
                TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto
            });
        }
    }

    class Root
    {
        public CodeDependency CodeDependency { get; set; }
    }

    class CodeDependency
    {
        public string Path { get; set; }
        public DependencyType Type { get; set; }
    }

    enum DependencyType
    {
        NONE = 0,
        TAR = 1,
        ZIP = 2,
        TAR_GZ = 3,
        DIRECTORY = 4,
    }

    class Program
    {


        static void Main(string[] args)
        {
            string json = "{\"CodeDependency\": { \"Path\": \"/CAP_TEST/job_manager/modules/1c8185d5-2add-4bd4-a332-8b21a6819608/tmpr9z7xinh.tar.gz\", \"Type\": \"TAR_GZ\" } }";


            var obj = json.DeserializeJson<Root>();

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}
Felix Arnold
  • 839
  • 7
  • 35
  • Thanks Max. I've tried JsonConvert.DeserializeObject. For class(and enum) defined in common cs file, it works fine. But for class defined in *.proto(and compiled to cs by protobuf dll), it doesn't work – user9345277 Dec 02 '19 at 02:10