Found 7442 Articles for Java

What to use @SerializedName annotation using Gson in Java?

raja
Updated on 09-Jul-2020 08:17:53

8K+ Views

The @SerializedName annotation can be used to serialize a field with a different name instead of an actual field name. We can provide the expected serialized name as an annotation attribute, Gson can make sure to read or write a field with the provided name.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.*; import com.google.gson.annotations.*; public class SerializedNameTest {    public static void main(String args[]) {       Gson gson = new GsonBuilder().setPrettyPrinting().create();       Person person = new Person(115, "Raja Ramesh", "Hyderabad");       String jsonStr = gson.toJson(person);       System.out.println(jsonStr);    } } // Person class class ... Read More

How to implement custom JsonAdapter using Gson in Java?

raja
Updated on 09-Jul-2020 08:15:08

3K+ Views

The @JsonAdapter annotation can be used at field or class level to specify the Gson. The TypeAdapter class can be used to convert Java objects to and from JSON. By default, Gson library converts application classes to JSON by using built-in type adapters but we can override it by providing custom type adapters.Syntax@Retention(value=RUNTIME) @Target(value={TYPE, FIELD}) public @interface JsonAdapterExampleimport java.io.IOException; import com.google.gson.Gson; import com.google.gson.TypeAdapter; import com.google.gson.annotations.JsonAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public class JsonAdapterTest {    public static void main(String[] args) {       Gson gson = new Gson();       System.out.println(gson.toJson(new Customer()));    } } // Customer class class Customer { ... Read More

Matcher quoteReplacement(String s) method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:43:12

523 Views

The appendReplacement() method of the Matcher class accepts a StringBuffer object and a String (replacement string) as parameters and, appends the input data to the StringBuffer object, replacing the matched content with the replacement string.Internally, this method reads each character from the input string and appends the String buffer, whenever a match occurs it appends the replacement string instead of matched content part of the string to the buffer and, proceeds from the next position of the matched substring.While passing the replacement string to this method if you use “/” or “$” they will not be considered as regular characters ... Read More

Matcher hitEnd() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:38:51

108 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The hitEnd() method verifies whether the end of the input data reached during the previous match if so, it returns true else false. If this method returns true it indicates that more input data might change the result of the match.For example, if you are trying to match the last word of an input string to you using the regex “you$” and if your 1st input line ... Read More

Matcher useAnchoringBounds() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:32:57

154 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The anchoring bounds are used to match the region matches such as ^ and $. By default, a matcher uses anchoring bounds.The useAnchoringBounds() method of this class method accepts a boolean value and, if you pass true to this method the current matcher uses anchoring bounds and if you pass false to this method it uses non-anchoring bounds.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Trail ... Read More

Matcher hasTransparentBounds() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:29:16

89 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.In regular expressions, the lookbehind and lookahead constructs are used to match a particular pattern that is preceding or, succeeding in some other pattern. For example, if you need to accept a string which accepts 5 to 12 characters the regular expression will be −"\A(?=\w{6, 10}\z)";By default, the boundaries of the matcher region are not transparent to the constructs lookahead, lookbehind and, boundary matching i.e. These constructs ... Read More

Matcher hasAnchoringBounds() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:24:47

110 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The anchoring bounds are used to match the region matches such as ^ and $. By default a matcher uses anchoring bounds, you can switch from using anchoring bounds to non-anchoring bounds using the useAnchoringBounds() method.The hasAnchoringBounds() method of this (Matcher) class verifies whether the current matcher uses anchoring bounds if so, it returns true else, it returns false.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class HasAnchoringBoundsExample { ... Read More

Matcher lookingAt() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:20:14

133 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The lookingAt() method of the Matcher class matches the given input text with the pattern, starting from the beginning of the region. In case of a match, this method returns true, else, false. Unlike the matches() method this method doesn’t require a match in the entire region to return true.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main(String[] args) {     ... Read More

Matcher toMatchResult() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:17:12

283 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The toMatchResult() method of this (Matcher) returns the match state of the current matcher.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class ToMatchResultExample {    public static void main(String[] args) {       String str = "This is an example.";       //Regular expression to match contents of the bold tags       String regex = "(\S+)";       //Creating a pattern object     ... Read More

Matcher regionEnd() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 07:10:54

140 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.The regionEnd() method of this (Matcher) class returns an integer value representing the end index of the current matcher object.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegionEndExample {    public static void main(String[] args) {       String regex = "(.*)(\d+)(.*)";       String input = "This is a sample Text, 1234, with numbers in between.";       //Creating a pattern object     ... Read More

Advertisements