Laden...

Wie komplexe Parse-Methode testen

Letzter Beitrag vor 8 Jahren 9 Posts 2.936 Views
Wie komplexe Parse-Methode testen

Hallo,
ich habe simplere Parsing-Methoden immer mit Linq to Json getestet:


        public static IEnumerable<object[]> LocationResponsesWithInvalidRegion
        {
            get
            {
                yield return new object[] { new JObject(new JProperty("name", "Stuttgart"), new JProperty("country", "dee")).ToString() };
                yield return new object[] { new JObject(new JProperty("name", "New York"), new JProperty("country", "uss")).ToString() };
            }
        }

Ich finde es wesentlich übersichtlicher als Json direkt mit Text zu übergeben und auch das escapen der Anführungszeichen fällt weg. Bei einer Methode, die komplexeres Json verarbeitet, wäre es ein viel komplizierterer Aufbau. Außerdem bräuchte ich IEquatbable nur für die Unit-Tests. Gibt es eine Möglichkeit das einfacher zu testen oder lohnt sich dieser Aufwand?

Übersichtlicher wird das mMn so


        public static IEnumerable<object[]> LocationResponsesWithInvalidRegion
        {
            get
            {
                yield return new object[] {
                    new Dictionary<string, object>
                    {
                        ["name"] = "Stuttgart",
                        ["country"] = "dee"
                    }.ToJsonString() };

                yield return new object[] {
                    new Dictionary<string, object>
                    {
                        ["name"] = "New York",
                        ["country"] = "uss"
                    }.ToJsonString() };
            }
        }

    static class JsonExtension
    {
        public static string ToJsonString( this Dictionary<string, object> source )
        {
            var result = Newtonsoft.Json.JsonConvert.SerializeObject( source );

            return result;
        }
    }

gerade wenn es etwas komplexer wird


new Dictionary<string, object>
{
    ["name"] = "Stuttgart",
    ["country"] = "dee",
    ["nested"] = new Dictionary<string,object>
    {
        ["name"] = "Stuttgart",
        ["country"] = "dee",
        ["nested"] = new Dictionary<string,object>
        {
            ["name"] = "Stuttgart",
            ["country"] = "dee",
            ["nested"] = new Dictionary<string,object>
            {
                ["name"] = "Stuttgart",
                ["country"] = "dee",
            },
        },
    },
}.ToJsonString();

ergibt dann


{
   "name":"Stuttgart",
   "country":"dee",
   "nested":{
      "name":"Stuttgart",
      "country":"dee",
      "nested":{
         "name":"Stuttgart",
         "country":"dee",
         "nested":{
            "name":"Stuttgart",
            "country":"dee"
         }
      }
   }
}

Danke, es sieht auf jeden Fall übersichtlicher aus. Gibt es eine Möglichkeit so auch Json Arrays zu erstellen?

Ja (ist wohl zu offensichtlich) serialisiere einfach ein object[]

Werfe mal noch anonymous types in den Raum 😉


new 
{
	name = "Suttgart",
	country = "de",
	nested = new
	{
		name = "Suttgart",
		country = "de",
		nested = new
		{
			name = "Suttgart",
			country = "de"
		}
	}
}

@malignate

Ja, geht auch ... aber eben nicht immer. Die Konventionen für die Property-Namen sind bei C# und JSON nicht deckungsgleich und können dann dazu führen, dass man doch wieder ein Dictionary<string,object> verwenden muss.

Dann verwende ich lieber das, was immer funktioniert 😉

Warum?
Ich kann doch mit beiden Varianten die Namenskonventionen erfüllen; warum soll da Dictionary besser / genauer sein? Beispiel?
Ich tendiere auch zu anonymous types.

RealLife-Beispiel


{
    "system": [
        {
            "id": "1000",
            "owner": "fred",
            "isSystemAvatar": true,
            "isSelected": false,
            "isDeletable": false,
            "urls": {
                "16x16": "http://localhost:8090/jira/secure/useravatar?size=xsmall&avatarId=10040",
                "24x24": "http://localhost:8090/jira/secure/useravatar?size=small&avatarId=10040",
                "32x32": "http://localhost:8090/jira/secure/useravatar?size=medium&avatarId=10040",
                "48x48": "http://localhost:8090/jira/secure/useravatar?avatarId=10040"
            },
            "selected": false
        }
    ]
}

Mit einem anonymen Typ geht das wie?

Mit dem Dictionary<string,object> mache ich das einfach ohne großes Nachdenken.

Update

Beliebt ist auch


{
  "profile-id":1234, 
  "user_id":6789
}

Beispiel aus der Philips-Hue-API:


[
    {
        "success":
         {
               "/lights/1/state/hue": 254
         }
     }
]

(ja, eine URI als Propertyname. Sehr geil.)

LaTino

"Furlow, is it always about money?"
"Is there anything else? I mean, how much sex can you have?"
"Don't know. I haven't maxed out yet."
(Furlow & Crichton, Farscape)