title: Azure PubSub / JSON subprotocol / deserialize JSON payload
body: <p>So JSON is going to be around for awhile, and I'm going to have to learn how it works things out....</p>
  <p>In Azure pubsub, I send a message like this:</p>
  <pre><code> var json = System.Text.Json.JsonSerializer.Serialize(&lt;&lt;a class in C#&gt;&gt; );
   await webPubSubServiceClient.SendToAllAsync(content: json, contentType: 
    ContentType.ApplicationJson);
  </code></pre>
  <p>When I get it on the other side (as its read from ResponseMessage.TEXT), I get this:</p>
  <pre><code>{
    &quot;type&quot;: &quot;message&quot;,
    &quot;from&quot;: &quot;server&quot;,
    &quot;dataType&quot;: &quot;json&quot;, //binary/json/text
    &quot;data&quot;: {
      &quot;event&quot;: &quot;connected&quot;,
      &quot;userId&quot;: &quot;WPFClient_162035&quot;,
      &quot;connectionId&quot;: &quot;&quot;,
      &quot;message&quot;: &quot;User: &gt;WPFClient_162035&lt; has joined hub:&gt;chat&lt;&quot;,
      &quot;type&quot;: &quot;system&quot;,
      &quot;group&quot;: &quot;&quot;,
      &quot;typeName&quot;: &quot;WebhostGeneratedMessage&quot;
    }
  }
  </code></pre>
  <p>The webPubSubServiceClient has wrapped the JSON I created within this structure:</p>
  <pre><code> {
        &quot;type&quot;: &quot;message&quot;,
        &quot;from&quot;: &quot;server&quot;,
        &quot;dataType&quot;: &quot;json&quot;, //binary/json/text
        &quot;data&quot;: {}
  </code></pre>
  <p>The problem with this is that I'm sending various JSON payloads, and the wrapper does not have an envelope field TELLing me how to deserialize the contents of what is in &quot;data&quot; - as within that class I have a base class with a property that fills in the typeName that I'm using.</p>
  <p>So when I do this:</p>
  <pre><code>TypeOfMessageReceived? deserialized = System.Text.Json.JsonSerializer.Deserialize&lt;TypeOfMessageReceived&gt;(body);
  </code></pre>
  <p>The deserialize has the change of blowing up on the data element, as the data element can be any class that I've serialized.</p>
  <p>So how can I read data with a class in C#? Do I read it as a JSONElement/JSONDocument and parse my way to it? If so, I would have hoped there were some examples of best practices.</p>
  <p>I think my other option is to simply define the message being sent as &quot;TEXT&quot;, and then deserialize to a base class, grab the segment I need, deserialize that fragment?</p>
  <p>I'm missing something here, as the JSON protocol should help me here, and not get in my way... or am I just thinking too strongly typed?</p>
  <p>I'm using NET6, so I'm hoping we have some new features I'm not aware of.  As I said, I'm looking for best practises using this new service.</p>
  <p>Thanks in advance.</p>