|
| 1 | + |
| 2 | +//And I saw thrones, and they sat upon them, and judgment was given unto them: |
| 3 | +//and I saw the souls of them that were beheaded for the witness of Jesus, and for the word of God, |
| 4 | +//and which had not worshipped the beast, neither his image, neither had received his mark upon their foreheads, |
| 5 | +//or in their hands; and they lived and reigned with Christ a thousand years. (Revelation 20:4) |
| 6 | + |
| 7 | +public class Solution { |
| 8 | + public static void main(String[] args) throws IOException { |
| 9 | + Second s = (Second) convertOneToAnother(new First(), Second.class); |
| 10 | + First f = (First) convertOneToAnother(new Second(), First.class); |
| 11 | + } |
| 12 | + |
| 13 | + public static Object convertOneToAnother(Object one, Class resultClassObject) throws IOException { |
| 14 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 15 | + StringWriter stringWriter = new StringWriter(); |
| 16 | + objectMapper.writeValue(stringWriter, one); |
| 17 | + |
| 18 | + String oneClassName = one.getClass().getSimpleName().toLowerCase(); |
| 19 | + String resClassName = resultClassObject.getSimpleName().toLowerCase(); |
| 20 | + String replaceStr = stringWriter.toString().replaceFirst(oneClassName, resClassName); |
| 21 | + |
| 22 | + return objectMapper.readValue(new StringReader(replaceStr), resultClassObject); |
| 23 | + } |
| 24 | + |
| 25 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property="className") |
| 26 | + @JsonSubTypes(@JsonSubTypes.Type(value=First.class, name="first")) |
| 27 | + public static class First { |
| 28 | + public int i; |
| 29 | + public String name; |
| 30 | + } |
| 31 | + |
| 32 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property="className") |
| 33 | + @JsonSubTypes(@JsonSubTypes.Type(value=Second.class, name="second")) |
| 34 | + public static class Second { |
| 35 | + public int i; |
| 36 | + public String name; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/* |
| 41 | +Конвертация из одного класса в другой используя JSON |
| 42 | +
|
| 43 | +НЕОБХОДИМО: подключенные библиотеки Jackson Core, Bind и Annotation версии 2.6.1 |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +Два класса имеют одинаковые поля, но не имеют общий суперкласс. Пример, классы First и Second. |
| 48 | +
|
| 49 | +Реализовать логику метода convertOneToAnother, который должен возвращать объект класса resultClassObject, значения полей которого равны значениям полей в объекте one. |
| 50 | +
|
| 51 | +Используй объект типа ObjectMapper. |
| 52 | +
|
| 53 | +Известно, что у классов есть JSON аннотация, у которой значение property равно имени класса в нижнем регистре. |
| 54 | +
|
| 55 | +На примере класса First, это className="first" |
| 56 | +
|
| 57 | +Классы First и Second не участвуют в тестировании, они предоставлены в качестве тестовых данных. |
| 58 | +
|
| 59 | +
|
| 60 | +
|
| 61 | +
|
| 62 | +
|
| 63 | +Требования: |
| 64 | +
|
| 65 | +1. Метод convertOneToAnother должен возвращать объект класса resultClassObject значения полей которого равны значениям полей объекта one. |
| 66 | +
|
| 67 | +2. В методе convertOneToAnother должен быть создан объект типа ObjectMapper. |
| 68 | +
|
| 69 | +3. Метод convertOneToAnother должен быть статическим. |
| 70 | +
|
| 71 | +4. Метод convertOneToAnother должен быть публичным. |
| 72 | +*/ |
0 commit comments