public Entity Deserialize(Stream input, Encoding encoding) { bool forceHypermedia; using (var reader = CreateReader(input, encoding, out forceHypermedia)) { GraphWriter graphWriter = null; using (var writer = new DelayedWriter()) { writer.Intercept += (o, e) => { if (e.Node.Type.HasFlag(NodeType.Property)) { var isPayload = "form".EqualsIgnoreCase(e.Node.Value as string) || "record".EqualsIgnoreCase(e.Node.Value as string) || "records".EqualsIgnoreCase(e.Node.Value as string); graphWriter = isPayload ? new GraphWriter(typeof(Payload)) : new GraphWriter(typeof(Entity)); writer.SetWriter(graphWriter); } }; reader.CopyTo(writer); } var graph = graphWriter?.Graphs.Cast <object>().FirstOrDefault(); var entity = graph as Entity ?? throw new NotImplementedException(); //var entity = graph is Payload payload ? payload.ToEntity() : (Entity)graph; return(entity); } }