0

I have a custom class that has some properties from an external dll ( i cannot change this dll class)

If property's class has an empty constructer then this property is serialized but if there is only one constructer that you have to give parameters then it cannot serialize.

For example I created an instance from this class and tried to serialize and result is "[]"

here is the class

public class Shape : ShapeBase,    
{
 public Shape(DocumentBase doc, ShapeType shapeType);    
 public Chart Chart { get; }
 public bool ExtrusionEnabled { get; }
 public Fill Fill { get; }
 public Color FillColor { get; set; }
 public bool Filled { get; set; }
 public Paragraph FirstParagraph { get; }
 public bool HasChart { get; }
 public bool HasImage { get; }
 public ImageData ImageData { get; }
 public Paragraph LastParagraph { get; }
 public override NodeType NodeType { get; }
 public OleFormat OleFormat { get; }
 public bool ShadowEnabled { get; }
 public SignatureLine SignatureLine { get; }
 public StoryType StoryType { get; }
 public Stroke Stroke { get; }
 public Color StrokeColor { get; set; }
 public bool Stroked { get; set; }
 public double StrokeWeight { get; set; }
 public TextBox TextBox { get; }
 public TextPath TextPath { get; }
 public override bool Accept(DocumentVisitor visitor);

}

and i try to serialize like that

    Shape shape = new Shape(_wordDocument, ShapeType.TextPlainText);

    string json = JsonConvert.SerializeObject(shape, Formatting.Indented);

but other class is like

public class PdfSaveOptions : FixedPageSaveOptions
{

    public PdfSaveOptions();
    public int BookmarksOutlineLevel { get; set; }
    public PdfCompliance Compliance { get; set; }
    public bool CreateNoteHyperlinks { get; set; }
    public PdfCustomPropertiesExport CustomPropertiesExport { get; set; }
    public PdfDigitalSignatureDetails DigitalSignatureDetails { get; set; }
    public override DmlEffectsRenderingMode DmlEffectsRenderingMode { get; set; }
    public bool DownsampleImages { get; set; }
    public DownsampleOptions DownsampleOptions { get; }
    .......
}

it is serialized.

So I also tried to use some options like

 ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor

but did not work.

unbalanced
  • 1,192
  • 5
  • 19
  • 44

1 Answers1

0

You still need to have constructor without parameters. Otherwise Deserialize method is not able to create instance of that object. Deserialize tryies to instantiate object and then use Properties or Fields (depends on configuration) to set data to it.

You can make your constructor private or internal if you want, just so long as its parameterless. You then have kind of constructor that doesn't exists outside your class.

public class Shape : ShapeBase,    
{
 private Shape() {
   //here some inits
 }
 public Shape(DocumentBase doc, ShapeType shapeType);    
 public Chart Chart { get; }
 public bool ExtrusionEnabled { get; }
 public Fill Fill { get; }
 public Color FillColor { get; set; }
 public bool Filled { get; set; }
 public Paragraph FirstParagraph { get; }
 public bool HasChart { get; }
 public bool HasImage { get; }
 public ImageData ImageData { get; }
 public Paragraph LastParagraph { get; }
 public override NodeType NodeType { get; }
 public OleFormat OleFormat { get; }
 public bool ShadowEnabled { get; }
 public SignatureLine SignatureLine { get; }
 public StoryType StoryType { get; }
 public Stroke Stroke { get; }
 public Color StrokeColor { get; set; }
 public bool Stroked { get; set; }
 public double StrokeWeight { get; set; }
 public TextBox TextBox { get; }
 public TextPath TextPath { get; }
 public override bool Accept(DocumentVisitor visitor);

}
madoxdev
  • 3,770
  • 1
  • 24
  • 39
  • I think you missunderstand me. This class is not my class. A dll is referenced therefore i cannot change this class structer – unbalanced Dec 16 '16 at 11:14
  • what if you create your own class that inherits from `Shape` and try to deserialize it as your type as opposed to original class? – madoxdev Dec 16 '16 at 11:15
  • I also tried it but it does not allow me to create without the same constructor . Then i called the same constructer as : base(...,...) but it did not work also. I mean it returns empty – unbalanced Dec 16 '16 at 11:17
  • your derivered class should call `parameter` constructor from `parameterless` constructor. Have you tried it that way? – madoxdev Dec 16 '16 at 11:24
  • sorry i was outside. Yes I tried like that and not working – unbalanced Dec 16 '16 at 12:44