I want to serialize FileInfo
object using json.
Till now, I am getting below result upon serializing file info object using JsonConvert.serializeOjbect()
method.
FileInfo finfo = new FileInfo("drive:\\folderpath\\file.txt");
string jsonString = JsonConvert.SerializeObject(finfo);
Of course, finfo
holds a lot more properties than the two held by jsonString.
(Properties like Exists
, Directory
, CreationTime
, Extension
, IsReadonly
, LastAccessTime
, etc.)
jsonstring
only holds two of these properties.
jsonString = {{
"OriginalPath": "drive:\\folderpath\\file.txt",
"FullPath": "drive:\\folderpath\\file.txt"
}}
Is there a way to serialize whole object instead of these two properties using Json?
The reason that you're only seeing those two properties is because FileInfo
implements the ISerializable
interface, and its GetObjectInfo
method is being used to override the default serialization behavior. If you had full control over the FileInfo
class (which you don't) then you could apply the [JsonObject] attribute to force normal Json serialization behavior.
I think your only option in this case to write your own wrapper containing the properties that you want. Your properties can just call the underlying FileInfo
properties directly.