The FieldNamingStrategy is a mechanism for providing custom field naming in Gson. This allows the client code to translate field names into a particular convention that is not supported as a normal Java field declaration rules. The translateName() method will prefix every field name with the string “pre_”.In the below example, we can implement the Custom FieldNamingStrategy.Exampleimport java.lang.reflect.Field; import com.google.gson.*; public class GsonFieldNamingStrategyTest { public static void main(String[] args) { Employee emp = new Employee(); emp.setEmpId(115); emp.setFirstName("Adithya"); emp.setLastName("Jai"); CustomFieldNamingStrategy customFieldNamingStrategy = new CustomFieldNamingStrategy(); ... Read More
All the default settings of JSON Parser can be represented using the JsonParser.Feature enumeration. The JsonParser.Feature.values() will return all the features that are available for JSONParser but whether a feature is enabled or disabled for a particular parser can be determined using the isEnabled() method of JsonParser. Syntaxpublic static enum JsonParser.Feature extends EnumExampleimport com.fasterxml.jackson.core.*; import java.io.*; public class JsonParserSettingsTest { public static void main(String[] args) throws IOException { String json = "[{\"name\":\"Adithya\", \"age\":\"30\"}, " + "{\"name\":\"Ravi\", \"age\":\"35\"}]"; JsonFactory jsonFactory = new JsonFactory(); JsonParser jsonParser = jsonFactory.createParser(json); for(JsonParser.Feature feature : JsonParser.Feature.values()) { ... Read More
The JsonFactory class is a thread-safe and responsible for creating instances of writer and reader. The list of settings that can be turned on/off is present in an enumeration JsonFactory.Feature, it contains static method values() that return the enum constant of this type with the specified name.Syntaxpublic static enum JsonFactory.Feature extends EnumExampleimport com.fasterxml.jackson.core.JsonFactory; public class JsonFactorySettingsTest { public static void main(String[] args) { JsonFactory jsonFactory = new JsonFactory(); for(JsonFactory.Feature feature : JsonFactory.Feature.values()) { boolean result = jsonFactory.isEnabled(feature); System.out.println(feature.name() + ":" + result); } } }OutputINTERN_FIELD_NAMES:true ... Read More
The JsonGenerator class can be responsible for writing JSON data as a stream instead of constructing an Object Model in memory. The list of settings that can be turned on/off is present in an enum JsonGenerator.Feature, it contains static method values() that returns an array containing the constants of this enum type.Syntaxpublic static enum JsonGenerator.Feature extends EnumExampleimport java.io.*; import com.fasterxml.jackson.core.*; public class JsonGeneratorSettingsTest { public static void main(String[] args) throws IOException { StringWriter writer = new StringWriter(); JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer); for(JsonGenerator.Feature feature : JsonGenerator.Feature.values()) { ... Read More
Gson library provides the naming conventions as part of enum FieldNamingPolicy. We can set the field naming policy using the setFieldNamingPolicy() method of the GsonBuilder class.FieldNamingPolicy enum ConstantsIDENTITY − Using this naming policy, the field name is unchanged.LOWER_CASE_WITH_DASHES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by a dash (-).LOWER_CASE_WITH_UNDERSCORES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by an underscore (_).UPPER_CAMEL_CASE − Using this naming policy, ... Read More
The JSONPointer is a standard that defines a string syntax that can be used to access a particular key value in the JSON document. An instance of JSONPointer can be created by calling the static factory method createPointer() on the Json class. In the JSONPointer, every string syntax is prefixed with “/”. We can get the value of a key by calling the getValue() method on the JsonPointer object.JSON fileExampleimport javax.json.*; import java.io.*; public class JsonPointerTest { public static void main(String[] args) throws Exception { JsonReader jsonReader = Json.createReader(new FileReader("simple.json")); JsonStructure jsonStructure = jsonReader.read(); JsonPointer jsonPointer1 ... Read More
The JsonPatch interface is a format for storing a sequence of operations that can be applied to the target JSON structure. There are few operations like add, remove, replace, copy, move and test can be stored in JsonPath and operated on JSON structure. The JsonPatchBuilder interface can be used for constructing a JSON patch using the Json.createPatchBuilder().JSON fileExampleimport java.io.*; import javax.json.Json; import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonReader; import javax.json.JsonStructure; public class JsonPatchTest { public static void main(String[] args) throws Exception { JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder(); JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build(); JsonReader reader = Json.createReader(new FileReader("simple.json")); ... Read More
This example demonstrates how do I disable back button in android while logging out the application.Step 1 − Create a new project in Android Studio, go to File rArr; New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.javaimport androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { ... Read More
This example demonstrates how do I Zoom In and Zoom Out an android ImageView.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.javaimport androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.ScaleGestureDetector; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ScaleGestureDetector scaleGestureDetector; private float mScaleFactor = 1.0f; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { ... Read More
This example demonstrates how do I disable Mobile data in Android.For your find Information, unless you have a rooted phone I don't think you can enable and disable data programmatically because in order to do so we have to include MODIFY_PHONE_STATE permission which is only given to system or signature apps.setMobileDataEnabled() method is no longer callable via reflection. It was callable since Android 2.1 (API 7) to Android 4.4 (API 19) via reflection, but as of Android 5.0 and later, even with the rooted phones, the setMobileDataEnabled() method is not callable.Step 1 − Create a new project in Android Studio, ... Read More