Bug 37242 - object is not initialised correctly
Summary: object is not initialised correctly
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: unspecified
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Manuel de la Peña [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2015-12-22 18:36 UTC by John Dormishian
Modified: 2016-02-26 16:07 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description John Dormishian 2015-12-22 18:36:06 UTC
Description of Problem:
Found this issue in the Working with the File System example:
   https://developer.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/
   https://developer.xamarin.com/samples/monotouch/FileSystemSampleCode/

In the following part of the above example:
		public static void WriteJson(UITextView display) {

			// Create a new record
			var account = new Account(){
			    Email = "monkey@xamarin.com",
			    Active = true,
			    CreatedDate = new DateTime(2015, 5, 27, 0, 0, 0, DateTimeKind.Utc),
			    Roles = new List<string> {"User", "Admin"}
			};

			// Serialize object
			var json = JsonConvert.SerializeObject(account, Newtonsoft.Json.Formatting.Indented);

			// Save to file
			var documents =	Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
			var filename = Path.Combine (documents, "account.json");
			File.WriteAllText(filename, json);

			// Display output
			display.Text = json;
		}

var json after execution of JsonVonvert... has value of '{}'
when looked at the account var in debugger, it is also not initialised.

if you reference any of the account members, for example :

			// Create a new record
			var account = new Account(){
			    Email = "monkey@xamarin.com",
			    Active = true,
			    CreatedDate = new DateTime(2015, 5, 27, 0, 0, 0, DateTimeKind.Utc),
			    Roles = new List<string> {"User", "Admin"}
			};
			var email = account.Email;
			// Serialize object
			var json = JsonConvert.SerializeObject(account, Newtonsoft.Json.Formatting.Indented);

now json has value of : 
{
  "Email": "monkey@xamarin.com"
}

Steps to reproduce the problem:
1. Download the sample code from
   https://developer.xamarin.com/samples/monotouch/FileSystemSampleCode/
2. run it using Mac book pro in Xamarin studio


Actual Results:
{}

Expected Results:
{
  "Email": "monkey@xamarin.com",
  "Active": true,
  "CreatedDate": "2015-05-27T00:00:00Z",
  "Roles": [
    "User",
    "Admin"
  ]
}

How often does this happen? 
The is a persistent issue.

Additional Information:

-
Comment 1 Sebastien Pouliot 2016-01-14 18:38:45 UTC
@Manuel can you confirm the issue and look if it's related to some settings (e.g. linker). thanks!
Comment 2 Manuel de la Peña [MSFT] 2016-02-24 10:35:27 UTC
I've been looking into this, the problem is quite simple (when talking about the logic of the issue, not the fix perse). The project is build with Link All YET the properties are never read, due to tht fact, the linker removes the get; from the properties. 

This means that when doing reflection, the json library tries to decide if the property is readable and gets a false from the CanRead property in the PropertyInfo and therefore does not serialize the property value. That is, there is no bug in the initialisation of the object BUT there is a bug in how agressive is the linker in this case, there are two way to test my logic:

1. Read one of the properties. The linker wont remove that one YET it will remove the others, meaning that the Json lib will only consider that one readable.
2. Change the Linker option from Link All to Link Frameworks Only or Dont Link and all properties will be readable.

@Sebastian Reflection + Link All + not calling some methods == dangerous atm.
Comment 3 Manuel de la Peña [MSFT] 2016-02-24 11:26:01 UTC
A PR with the correct link option has been added.
Comment 4 Oleg Demchenko 2016-02-26 16:07:19 UTC
Fix merged to monotouch-samples/master