Programming Articles - Page 2336 of 3366

How to convert a bean to JSON object using Exclude Filter in Java?

raja
Updated on 08-Jul-2020 11:20:06

492 Views

The JsonConfig class can be used to configure the serialization process. We can use the setJsonPropertyFilter() method of JsonConfig to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. It returns true if the property will be filtered out or false otherwise.Syntaxpublic void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.PropertyFilter; public class ConvertBeanToJsonExcludeFilterTest {    public static void main(String[] args) {       Student student = new Student("Sai", "Chaitanya", 20, "Hyderabad");       JsonConfig jsonConfig = new JsonConfig();       jsonConfig.setJsonPropertyFilter(new CustomPropertyFilter());       ... Read More

How to convert a bean to XML without type hints using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 11:20:50

306 Views

The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can convert a bean to XML without type hints using the setTypeHintsEnabled() method of XMLSerializer class, this method sets whether JSON types can be included as attributes. We can pass false as an argument to this method to disable the type hints in XML.Syntaxpublic void setTypeHintsEnabled(boolean typeHintsEnabled)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLNoHintsTest {    public static void main(String[] args) {       Employee emp = new Employee("Krishna Vamsi", 115, 30, "Java");       JSONObject jsonObj = JSONObject.fromObject(emp);     ... Read More

Char.ToLowerInvariant(Char) Method in C#

AmitDiwan
Updated on 06-Nov-2019 08:06:14

182 Views

The Char.ToLowerInvariant() method in C# is used to convert the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.OutputFollowing is the syntax −public static char ToLowerInvariant (char ch);Above, the parameter ch is the Unicode character to convert.ExampleLet us now see an example to implement the Char.ToLowerInvariant() method −using System; public class Demo {    public static void Main(){       char ch = 'D';       char res = Char.ToLowerInvariant(ch);       Console.WriteLine("Value = "+ch);       Console.WriteLine("Lowercase Equivalent = "+res);    } }OutputThis will produce the following ... Read More

Char.Parse(String) Method in C#

AmitDiwan
Updated on 06-Nov-2019 08:04:37

4K+ Views

The Char.Parse(String) method in C# is used to convert the value of the specified string to its equivalent Unicode character.SyntaxFollowing is the syntax −public static char Parse (string str);Above, the string str is a string that contains a single character or null.ExampleLet us now see an example to implement the Char.Parse(String) method −using System; public class Demo {    public static void Main(){       string str ="a";       char res = Char.Parse(str);       Console.WriteLine("Value = "+str);       Console.WriteLine("Equivalent Unicode character = "+res);    } }OutputThis will produce the following output −Value = ... Read More

Decimal.ToSingle() Method in C#

AmitDiwan
Updated on 06-Nov-2019 08:03:00

160 Views

The Decimal.ToSingle() method in C# is used to convert the value of the specified Decimal to the equivalent single-precision floating-point number.SyntaxFollowing is the syntax −public static float ToSingle (decimal val);Above, Val is the decimal number to convert.ExampleLet us now see an example to implement the Decimal.ToSingle() method −using System; public class Demo {    public static void Main(){       Decimal val = 0.00000007575833m;       Console.WriteLine("Decimal value = "+val);       float res = Decimal.ToSingle(val);       Console.WriteLine("Single-precision floating-point number = "+res);    } }OutputThis will produce the following output −Decimal value = 0.00000007575833 Single-precision ... Read More

Decimal.ToSByte() Method in C#

AmitDiwan
Updated on 06-Nov-2019 08:01:34

70 Views

The Decimal.ToSByte() method in C# is used to convert the value of the specified Decimal to the equivalent 8-bit signed integer.SyntaxFollowing is the syntax −public static sbyte ToSByte (decimal val);Above, Val is the decimal number to convert.ExampleLet us now see an example to implement the Decimal.ToSByte() method −using System; public class Demo {    public static void Main(){       Decimal val = 97.567m;       Console.WriteLine("Decimal value = "+val);       sbyte res = Decimal.ToSByte(val);       Console.WriteLine("SByte value = "+res);    } }OutputThis will produce the following output −Decimal value = 97.567 SByte value ... Read More

Decimal.ToOACurrency() Method in C#

AmitDiwan
Updated on 06-Nov-2019 07:59:34

95 Views

The Decimal.ToOACurrency() method in C# is used to convert the specified Decimal value to the equivalent OLE Automation Currency value, which is contained in a 64-bit signed integer.SyntaxFollowing is the syntax −public static long ToOACurrency (decimal val);Above, Val is the decimal number to convert.ExampleLet us now see an example to implement the Decimal.ToOACurrency() method −using System; public class Demo {    public static void Main(){       Decimal val = 95;       Console.WriteLine("Decimal value = "+val);       long res = Decimal.ToOACurrency(val);       Console.WriteLine("Long value = "+res);    } }OutputThis will produce the following ... Read More

Type.GetHashCode() Method in C#

AmitDiwan
Updated on 06-Nov-2019 07:15:11

143 Views

The Type.GetHashCode() method in C# is used to return the hash code for this instance.SyntaxFollowing is the syntax −public override int GetHashCode ();ExampleLet us now see an example to implement the Type.GetHashCode() method −using System; public class Demo {    public static void Main(){       string[] arr = {"tom", "amit", "kevin", "katie"};       Type t1 = arr.GetType();       Type t2 = t1.GetElementType();       Console.WriteLine("Type = "+t2.ToString());       Console.WriteLine("Hash Code = "+t1.GetHashCode());    } }OutputThis will produce the following output −Type = System.String Hash Code = 9144789ExampleLet us now see another ... Read More

Type.GetField() Method in C#

AmitDiwan
Updated on 06-Nov-2019 07:12:54

337 Views

The Type.GetField() method in C# is used to get a specific field of the current Type.SyntaxFollowing is the syntax −public System.Reflection.FieldInfo GetField (string name); public abstract System.Reflection.FieldInfo GetField (string name, System.Reflection.BindingFlags bindingAttr);Above, the name is the string containing the name of the data field to get. The bindingAttr parameter is the bitwise combination of the enumeration values that specify how the search is conducted.ExampleLet us now see an example to implement the Type.GetField() method −using System; using System.Reflection; public class Demo {    public static void Main(){       Type type = typeof(Subject);       try {   ... Read More

Type.GetEnumValues() Method in C#

AmitDiwan
Updated on 06-Nov-2019 07:10:29

367 Views

The Type.GetEnumValues() method in C# returns an array of the values of the constants in the current enumeration type.SyntaxFollowing is the syntax −public virtual Array GetEnumValues ();ExampleLet us now see an example to implement the Type.GetEnumValues() method −using System; public class Demo {    enum Vehicle {Car, Bus, Bike, Airplane}    public static void Main(){       try {          Type type = typeof(int);          string[] str = type.GetEnumNames();          Console.WriteLine("GetEnumNames() to return the constant name = " + str);          Type type2 = type.GetEnumUnderlyingType();     ... Read More

Advertisements