0

How to convert the string back to object? I have the following class:

class Test {
    String name;
    String value;
    String testing;

    @Override
    public String toString() {
        return "Test{" +
                "name='" + name + '\'' +
                ", value='" + value + '\'' +
                ", testing='" + testing + '\'' +
                '}';
    }
}

class Testing {
   private List<Test> testing = new ArrayList<Test>();
   handling(testing.toString())
   public String handling(String testing) {
        // do some handling and return a string
   }
}

ArrayList testing must handled by convert to string, for example, after that, we get the following string:

[Test{name='name1', value='value1', testing='testing1'}, Test{name='name2', value='value2', testing='testing2'}, Test{name='name3', value='value3', testing='testing3'}]

then how to convert the string back to object list of Test?

Can anyone help on it?

5
  • Not sure why you really need this, since you have the original List<Test> within scope, but you could always add a static utility method to your Test class that would de-serialize a given String representation back into a Test instance. Commented Nov 19, 2018 at 13:38
  • 3
    If this is for some kind of text-based api I'd recommend using JSON which is easier to parse especially since there are a lot of libraries for that. If you use your own custom text representation then you'll have to provide your own custom parser as well. Commented Nov 19, 2018 at 13:43
  • 2
    You shouldn't use toString() to serialize your objects. Instead of this just use Jackson to serialize objects to JSON and back from JSON to POJO. You objects should implement Serializable for it. Commented Nov 19, 2018 at 13:43
  • it is not Json actually Commented Nov 19, 2018 at 13:44
  • No, it is not JSON, but it could/should be. Otherwise you have to write a parser for it yourself. Commented Nov 19, 2018 at 13:45

2 Answers 2

1

If you don't need exactly that toString pattern, but only need to convert your Object to something human-readable and than back to an Object, you could marshal to json and parse back to Object seamlessly with something like Jackson ObjectMapper. See https://www.baeldung.com/jackson-object-mapper-tutorial for a quick-start.

Sign up to request clarification or add additional context in comments.

Comments

0

You can create an additional constructor and call them with the string:

class Test {
    private String name;
    private String value;
    private String testing;

    Test(string objString) { 
        Pattern pattern = Pattern.compile("name=('.+'), value=('.+'), testing=('.+')");
        Matcher matcher = pattern.matcher(objString);
        if (matcher.matches()) {
            name = matcher.group(1);
            value = matcher.group(2);
            testing = matcher.group(3);
        } else {
            throw new ArgumentException("Cannot parse"):
        }
    }

    @Override
    public String toString() {
        return "Test{" +
                "name='" + name + "'" +
                ", value='" + value + "'" +
                ", testing='" + testing + "'" +
                '}';
    }
}

class Testing {
    doTesting(List<Test> testing) {
        List<String> testingAsString = new ArrayList<String>();
        // To string
        for (Test t : testing) {
            testingAsString.add(t.toString());
        }
        List<Test> clonedTesting = new ArrayList<Test>();
        // To Test
        for (String t : testingAsString) {
            clonedTesting.add(new Test(t));
        }

        // Here every test string is again an object of type Test
        for (Test t : clonedTesting) {
            System.out.println(t);
        }
    }  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.